Home > Web Front-end > JS Tutorial > dateAdd and dateDiff methods of Javascript date objects_time and date

dateAdd and dateDiff methods of Javascript date objects_time and date

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:58:29
Original
1314 people have browsed it
复制代码 代码如下:

Date.prototype.dateAdd = function(interval,number)
{
var d = this;
var k={'y':'FullYear', 'q':'Month', 'm':'Month', 'w':'Date', 'd':'Date', 'h':'Hours', 'n':'Minutes', 's':'Seconds', 'ms':'MilliSeconds'};
var n={'q':3, 'w':7};
eval('d.set' k[interval] '(d.get' k[interval] '() ' ((n[interval]||1)*number) ')');
return d;
}
Date.prototype.dateDiff = function(interval,objDate2)
{
var d=this, i={}, t=d.getTime(), t2=objDate2.getTime();
i['y']=objDate2.getFullYear()-d.getFullYear();
i['q']=i['y']*4 Math.floor(objDate2.getMonth()/4)-Math.floor(d.getMonth()/4);
i['m']=i['y']*12 objDate2.getMonth()-d.getMonth();
i['ms']=objDate2.getTime()-d.getTime();
i['w']=Math.floor((t2 345600000)/(604800000))-Math.floor((t 345600000)/(604800000));
i['d']=Math.floor(t2/86400000)-Math.floor(t/86400000);
i['h']=Math.floor(t2/3600000)-Math.floor(t/3600000);
i['n']=Math.floor(t2/60000)-Math.floor(t/60000);
i['s']=Math.floor(t2/1000)-Math.floor(t/1000);
return i[interval];
}

dateAdd 方法
返回已添加指定时间间隔的日期对象。
dateObj.dateAdd(interval, number)
参数
dateObj
必选项。任意 Date 对象。
interval
必选项。字符串表达式,表示要添加的时间间隔。有关数值,请参阅“设置”部分。
number
必选项。数值表达式,表示要添加的时间间隔的个数。数值表达式可以是正数(得到未来的日期)或负数(得到过去的日期)。
dateDiff 方法
返回两个日期对象之间的时间间隔。
dateObj.dateDiff(interval, dateObj2)
参数
interval
必选项。字符串表达式,表示用于计算 date1 和 date2 之间的时间间隔。有关数值,请参阅“设置”部分。
dateObj, dateObj2
必选项。日期对象。用于计算的两个日期对象。
设置
interval 参数可以有以下值:
设置 描述
y
q 季度
m
d
w
h 小时
n 分钟
s
ms 毫秒
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template