前言
原生JS有些没定义的方法只能自己封装了,比如获取现在相隔本年过了几天以及过了多少周,这些都是原JS里没有的方法,现在插件只有一些方法,后期再慢慢扩展
插件使用方法
-
引用TimeToPack.js文件
-
调用_.GetOneYearToDay()得到本年已经过了几天
-
调用_.GetOneYearToDay(int Year,int Month,int Day) 参数(int年,int月,int日),通过参数得到相隔今日时间,3个参数可选填,比如_.GetOneYearToDay(2019,3)这样默认获取2019/3/1相隔今日时间
-
调用_.GetDayToWeek()得到现在是第几周
-
调用_.GetDayToWeek(int Day)通过参数得到周数
-
调用_.LeapYearJudgment()得到今年是否是闰年(返回true则是,返回false则不是)
-
调用_.LeapYearJudgment(int Year)通过参数获取是否是闰年(返回true则是,返回false则不是)
源代码
; (function (window, undefined) {
var
TimeToPack = function (e) {
return new TimeToPack.fn.init(e);
};
TimeToPack.fn = TimeToPack.prototype = {
};
var init = TimeToPack.fn.init = TimeToPack.prototype.TimeToPack = function (e) {
this.name = "init";
};
init.prototype = TimeToPack.prototype;
TimeToPack.GetOneYearToDay = function () {
var NowDate = new Date();
var SetDate = new Date();
var time1 = null, time2 = null, time_ = null;
if (arguments.length === 0) {
SetDate.setFullYear(new Date().getFullYear());
SetDate.setMonth(0);
SetDate.setDate(1);
} else if (arguments.length === 1) {
SetDate.setFullYear(arguments[0]);
SetDate.setMonth(0);
SetDate.setDate(1);
} else if (arguments.length === 2) {
SetDate.setFullYear(arguments[0]);
SetDate.setMonth(arguments[1] - 1);
SetDate.setDate(1);
} else if (arguments.length === 3) {
SetDate.setFullYear(arguments[0]);
SetDate.setMonth(arguments[1] - 1);
SetDate.setDate(arguments[2]);
}
time1 = Date.parse(SetDate.toLocaleDateString()) / 1000;
time2 = Date.parse(NowDate.toLocaleDateString()) / 1000;
time_ = time1 - time2;
console.log(time_, time1, time2);
if (time_ < 0) {
time_ -= (time_ * 2);
}
return (time_ / (3600 * 24)) + 1;
};
TimeToPack.GetDayToWeek = function () {
var target = arguments[0];
if (arguments.length === 0) {
target = _.GetOneYearToDay();
}
target /= 7;
return parseInt(target);
};
TimeToPack.LeapYearJudgment = function () {
var ADate = new Date();
var target = arguments[0];
if (arguments.length === 0) {
target = ADate.getFullYear();
}
target /= 4;
if (Math.floor(target) === target) {
return true;
}
return false;
}
window.TimeToPack = window._ = TimeToPack;
})(window, undefined);
源码解析
抽取一部分方法解析
TimeToPack.GetOneYearToDay = function () {
var NowDate = new Date();
var SetDate = new Date();
var time1 = null, time2 = null, time_ = null;
if (arguments.length === 0) {
SetDate.setFullYear(new Date().getFullYear());
SetDate.setMonth(0);
SetDate.setDate(1);
} else if (arguments.length === 1) {
SetDate.setFullYear(arguments[0]);
SetDate.setMonth(0);
SetDate.setDate(1);
} else if (arguments.length === 2) {
SetDate.setFullYear(arguments[0]);
SetDate.setMonth(arguments[1] - 1);
SetDate.setDate(1);
} else if (arguments.length === 3) {
SetDate.setFullYear(arguments[0]);
SetDate.setMonth(arguments[1] - 1);
SetDate.setDate(arguments[2]);
}
time1 = Date.parse(SetDate.toLocaleDateString()) / 1000;
time2 = Date.parse(NowDate.toLocaleDateString()) / 1000;
time_ = time1 - time2;
console.log(time_, time1, time2);
if (time_ < 0) {
time_ -= (time_ * 2);
}
return (time_ / (3600 * 24)) + 1;
};
这个GetOneYearToDay()方法就是返回过了本年的天数
声明变量
var NowDate = new Date();
var SetDate = new Date();
var time1 = null, time2 = null, time_ = null;
arguments是获取参数
没有参数传入会返回长度为0的数组,有参数传入就会返回N个参数长度
判断
if (arguments.length === 0) {}
如果arguments返回的长度强等于0就会执行下面的代码,也就是GetOneYearToDay()不给参数就会执行这里的代码
SetDate.setFullYear(new Date().getFullYear()); //设置SetDate的年份为本年
SetDate.setMonth(0);//设置SetDate的月份为1月,因为月份是有索引的索引0就是1月
SetDate.setDate(1);设置SetDate的日期为1日
其他判断以此类推
time1 = Date.parse(SetDate.toLocaleDateString()) / 1000;
time2 = Date.parse(NowDate.toLocaleDateString()) / 1000;
time_ = time1 - time2;
前面判断完成就给每个变量赋值
Date.parse()把括号里的日期转为毫秒
SetDate.toLocaleDateString()获取当前日期的字符串
Date.parse(SetDate.toLocaleDateString()) / 1000 得到秒数
time1 – time2 得到相差值
再次判断
if (time_ < 0) {
time_ -= (time_ * 2);
}
如果 time_值为负数 则 time_ 减它(本身 * 2)再赋值 就会返回正数
return (time_ / (3600 * 24)) + 1;
最后返回 time_ / (3600 * 24) + 1 得到相差天数
END