2014-07-26 08:30:52|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:JavaScript|来源:唯设编程网
//+--------------------------------------------------- //| 日期计算 //+--------------------------------------------------- Date.prototype.DateAdd = function(strInterval, Number) { var dtTmp = this; switch (strInterval) { case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number)); case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number)); case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number)); case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number)); case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); } }
//+--------------------------------------------------- //| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串 //+--------------------------------------------------- Date.prototype.DateDiff = function(strInterval, dtEnd) { var dtStart = this; if (typeof dtEnd == 'string' )//如果是字符串转换为日期型 { dtEnd = StringToDate(dtEnd); } switch (strInterval) { case 's' :return parseInt((dtEnd - dtStart) / 1000); case 'n' :return parseInt((dtEnd - dtStart) / 60000); case 'h' :return parseInt((dtEnd - dtStart) / 3600000); case 'd' :return parseInt((dtEnd - dtStart) / 86400000); case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7)); case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1); case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear(); } }
//+--------------------------------------------------- //| 日期输出字符串,重载了系统的toString方法 //+--------------------------------------------------- Date.prototype.toString = function(showWeek) { var myDate= this; var str = myDate.toLocaleDateString(); if (showWeek) { var Week = ['日','一','二','三','四','五','六']; str += ' 星期' + Week[myDate.getDay()]; } return str; }
//+--------------------------------------------------- //| 把日期分割成数组 //+--------------------------------------------------- Date.prototype.toArray = function() { var myDate = this; var myArray = Array(); myArray[0] = myDate.getFullYear(); myArray[1] = myDate.getMonth(); myArray[2] = myDate.getDate(); myArray[3] = myDate.getHours(); myArray[4] = myDate.getMinutes(); myArray[5] = myDate.getSeconds(); return myArray; }