首页 > JavaScript脚本, 前端开发 > 最近写日期操作的几个JS函数

发表评论 阅读评论 最近写日期操作的几个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))
}
  1. 2010年4月25日15:10 | #1

    踩过,终于又看到新文章了。每次来都能学到新东西,希望下篇文章尽快出来,HOHO。

  2. 2010年4月25日08:02 | #2

    看不错函数的作用

  3. 2010年4月25日04:44 | #3

    看不错函数的作用

  4. 2010年4月24日18:11 | #4

    踩过,终于又看到新文章了。每次来都能学到新东西,希望下篇文章尽快出来,HOHO。

  5. 2010年4月24日16:16 | #5

    恩,不错,这些方面我的意见在我博客里也写过,可以交流一下。希望看到更好的文章

  6. 2010年4月23日23:31 | #6

    看不错函数的作用

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

您也可以使用微博账号登陆