JavaScript实现日期加减计算功能代码实例,因为在js中没有类似C#中的AddDays方法,所以要想实现日期加减的话,就需要自己写函数来实现。这里就是通过重写Js Date对象的prototype,扩展了增加日期的方法,方便日后使用:
01
<script language="javascript">
02
Date.prototype.Format = function(fmt)
03
{
04
//代码作者: meizz
05
var o =
06
{
07
"M+" : this.getMonth() + 1, //月份
08
"d+" : this.getDate(), //日
09
"h+" : this.getHours(), //小时
10
"m+" : this.getMinutes(), //分
11
"s+" : this.getSeconds(), //秒
12
"q+" : Math.floor((this.getMonth() + 3) / 3), //季度
13
"S" : this.getMilliseconds() //毫秒
14
};
15
if (/(y+)/.test(fmt))
16
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
17
for (var k in o)
18
if (new RegExp("(" + k + ")").test(fmt))
19
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr((