6 条评论最近写日期操作的几个JS函数
2010年4月23日
//格式化函数 Date原型扩展
Date.prototype.format = function(format)
{
var o = {
"M+" : this.getMonth()+1, // month
"d+" : this.getDate(),// day
"h+" : this.getHours(), // hour
"m+" : this.getMinutes(), // minute
"s+" : this.getSeconds(), // second
"q+" : Math.floor((this.getMonth()+3)/3), // quarter
"S" : this.getMilliseconds() // millisecond
}
if(/(y+)/.test(format))
format = format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k]
"00"+ o[k]).substr((""+ o[k]).length));
return format;
}
// 日期比较
function datecompare(startDate, endDate) {
var re = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
var d1 = new Date(startDate.replace(/-/g, "/"));
var d2 = new Date(endDate.replace(/-/g, "/"));
if (Date.parse(d1) - Date.parse(d2) == 0) {
// window.alert("两个日期相等");
return "1";
}
if (Date.parse(d1) - Date.parse(d2) < 0) {
// window.alert("结束日期 大于 开始日期");
return "2";
}
if (Date.parse(d1) - Date.parse(d2) > 0) {
// window.alert("结束日期 小于 开始日期");
return "3";
}
}
//添加一天
function DateAddOneDay(now){
return new Date(Date.parse(now) + (86400000 * 1))
}

