自己整理的一个javascript日期处理函数
程序员文章站
2022-11-09 11:43:33
复制代码 代码如下: /* * 函数名称: dateutil * 作 者: yithcn * 功能说明: 日期函数 * 使用说明: * 创建日期: 2010.10.14 *...
复制代码 代码如下:
/*
* 函数名称: dateutil
* 作 者: yithcn
* 功能说明: 日期函数
* 使用说明:
* 创建日期: 2010.10.14
*/
var dateutil = {};
dateutil.base = 60 * 60 * 24 * 1000;
dateutil.add = function(num, sdate) {
num = num || 0;
sdate = sdate || new date();
var base = this.base * num;
var todayms = sdate.gettime();
todayms += base;
sdate.settime(todayms);
var m = (sdate.getmonth() + 1);
m = m < 10 ? "0" + m : m;
var d = sdate.getdate();
d = d < 10 ? "0" + d : d;
var y = sdate.getfullyear();
return m + "/" + d + "/" + y;
};
dateutil.diff = function(sdate, edate, mode) {
if (typeof sdate == "string")
sdate = new date(sdate);
if (typeof edate == "string")
edate = new date(edate);
sdate = sdate || new date();
edate = edate || new date();
try {
sdate.getyear();
} catch (e) {
return (0);
}
var result = math.abs(edate - sdate);
switch (mode) {
case "y":
result /= this.base * 365;
break;
case "m":
result /= this.base * 365 / 12;
break;
case "w":
result /= this.base * 7;
break;
default:
result /= this.base;
break;
}
return (math.floor(result));
};
dateutil.time = function(hassec) {
var date = new date();
return date.gethours() + ":" + date.getminutes() + (hassec ? ":" + date.getseconds() : "");
};
dateutil.timesplit = function(hassec) {
var date = new date();
return { hour: date.gethours(), minute: date.getminutes(), second: (hassec ? ":" + date.getseconds() : "") };
};