« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
|
Blog信息 |
blog名称:流浪狗 日志总数:44 评论数量:19 留言数量:-2 访问次数:190644 建立时间:2008年3月13日 |

| |
[JavaScript][转]js日期时间函数(经典+完善+实用) 软件技术
流浪狗 发表于 2008/7/29 15:10:29 |
来自http://new.dlgzy.com/bbs/show.asp?id=42&bd=8&totable=1日期时间脚本库方法列表Date.prototype.isLeapYear 判断闰年Date.prototype.Format 日期格式化Date.prototype.DateAdd 日期计算Date.prototype.DateDiff 比较日期差Date.prototype.toString 日期转字符串Date.prototype.toArray 日期分割为数组Date.prototype.DatePart 取日期的部分信息Date.prototype.MaxDayOfDate 取日期所在月的最大天数Date.prototype.WeekNumOfYear 判断日期所在年的第几周StringToDate 字符串转日期型IsValidDate 验证日期有效性CheckDateTime 完整日期时间检查daysBetween 日期天数差js代码:
500)this.width=500'>//--------------------------------------------------- 500)this.width=500'>// 判断闰年 500)this.width=500'>//--------------------------------------------------- 500)this.width=500'>Date.prototype.isLeapYear = function() 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))); 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//--------------------------------------------------- 500)this.width=500'>// 日期格式化 500)this.width=500'>// 格式 YYYY/yyyy/YY/yy 表示年份 500)this.width=500'>// MM/M 月份 500)this.width=500'>// W/w 星期 500)this.width=500'>// dd/DD/d/D 日期 500)this.width=500'>// hh/HH/h/H 时间 500)this.width=500'>// mm/m 分钟 500)this.width=500'>// ss/SS/s/S 秒 500)this.width=500'>//--------------------------------------------------- 500)this.width=500'>Date.prototype.Format = function(formatStr) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var str = formatStr; 500)this.width=500'> var Week = ['日','一','二','三','四','五','六']; 500)this.width=500'> 500)this.width=500'> str=str.replace(/yyyy|YYYY/,this.getFullYear()); 500)this.width=500'> str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); 500)this.width=500'> 500)this.width=500'> str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth()); 500)this.width=500'> str=str.replace(/M/g,this.getMonth()); 500)this.width=500'> 500)this.width=500'> str=str.replace(/w|W/g,Week[this.getDay()]); 500)this.width=500'> 500)this.width=500'> str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate()); 500)this.width=500'> str=str.replace(/d|D/g,this.getDate()); 500)this.width=500'> 500)this.width=500'> str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours()); 500)this.width=500'> str=str.replace(/h|H/g,this.getHours()); 500)this.width=500'> str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes()); 500)this.width=500'> str=str.replace(/m/g,this.getMinutes()); 500)this.width=500'> 500)this.width=500'> str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds()); 500)this.width=500'> str=str.replace(/s|S/g,this.getSeconds()); 500)this.width=500'> 500)this.width=500'> return str; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 求两个时间的天数差 日期格式为 YYYY-MM-dd 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>function daysBetween(DateOne,DateTwo) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); 500)this.width=500'> var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); 500)this.width=500'> var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); 500)this.width=500'> 500)this.width=500'> var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); 500)this.width=500'> var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); 500)this.width=500'> var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); 500)this.width=500'> 500)this.width=500'> var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000); 500)this.width=500'> return Math.abs(cha); 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 日期计算 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>500)this.width=500'>Date.prototype.DateAdd = function(strInterval, Number) 500)this.width=500'>{ 500)this.width=500'> var dtTmp = this; 500)this.width=500'>500)this.width=500'> switch (strInterval) 500)this.width=500'>{ 500)this.width=500'> case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number)); 500)this.width=500'> case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number)); 500)this.width=500'> case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number)); 500)this.width=500'> case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number)); 500)this.width=500'> case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); 500)this.width=500'> case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); 500)this.width=500'> case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); 500)this.width=500'> case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); 500)this.width=500'> } 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>500)this.width=500'>Date.prototype.DateDiff = function(strInterval, dtEnd) 500)this.width=500'>{ 500)this.width=500'> var dtStart = this; 500)this.width=500'> if (typeof dtEnd == 'string' )//如果是字符串转换为日期型 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> dtEnd = StringToDate(dtEnd); 500)this.width=500'> } 500)this.width=500'>500)this.width=500'> switch (strInterval) 500)this.width=500'>{ 500)this.width=500'> case 's' :return parseInt((dtEnd - dtStart) / 1000); 500)this.width=500'> case 'n' :return parseInt((dtEnd - dtStart) / 60000); 500)this.width=500'> case 'h' :return parseInt((dtEnd - dtStart) / 3600000); 500)this.width=500'> case 'd' :return parseInt((dtEnd - dtStart) / 86400000); 500)this.width=500'> case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7)); 500)this.width=500'> case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1); 500)this.width=500'> case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear(); 500)this.width=500'> } 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 日期输出字符串,重载了系统的toString方法 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>Date.prototype.toString = function(showWeek) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var myDate= this; 500)this.width=500'> var str = myDate.toLocaleDateString(); 500)this.width=500'> if (showWeek) 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> var Week = ['日','一','二','三','四','五','六']; 500)this.width=500'> str += ' 星期' + Week[myDate.getDay()]; 500)this.width=500'> } 500)this.width=500'> return str; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 日期合法性验证 500)this.width=500'>//| 格式为:YYYY-MM-DD或YYYY/MM/DD 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>function IsValidDate(DateStr) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格; 500)this.width=500'> if(sDate=='') return true; 500)this.width=500'> //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为'' 500)this.width=500'> //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式 500)this.width=500'>500)this.width=500'> var s = sDate.replace(/[\d]500)this.width=500'>{ 4,4 }[\-/]500)this.width=500'>{ 1 }[\d]500)this.width=500'>{ 1,2 }[\-/]500)this.width=500'>{ 1 }[\d]500)this.width=500'>{ 1,2 }/g,''); 500)this.width=500'> if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> var t=new Date(sDate.replace(/\-/g,'/')); 500)this.width=500'> var ar = sDate.split(/[-/:]/); 500)this.width=500'> if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate()) 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); 500)this.width=500'> return false; 500)this.width=500'> } 500)this.width=500'> } 500)this.width=500'> else 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); 500)this.width=500'> return false; 500)this.width=500'> } 500)this.width=500'> return true; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 日期时间检查 500)this.width=500'>//| 格式为:YYYY-MM-DD HH:MM:SS 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>function CheckDateTime(str) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'>500)this.width=500'> var reg = /^(\d+)-(\d500)this.width=500'>{ 1,2 })-(\d500)this.width=500'>{ 1,2 }) (\d500)this.width=500'>{ 1,2 }):(\d500)this.width=500'>{ 1,2 }):(\d500)this.width=500'>{ 1,2 })$/; 500)this.width=500'> var r = str.match(reg); 500)this.width=500'> if(r==null)return false; 500)this.width=500'> r[2]=r[2]-1; 500)this.width=500'> var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]); 500)this.width=500'> if(d.getFullYear()!=r[1])return false; 500)this.width=500'> if(d.getMonth()!=r[2])return false; 500)this.width=500'> if(d.getDate()!=r[3])return false; 500)this.width=500'> if(d.getHours()!=r[4])return false; 500)this.width=500'> if(d.getMinutes()!=r[5])return false; 500)this.width=500'> if(d.getSeconds()!=r[6])return false; 500)this.width=500'> return true; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 把日期分割成数组 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>Date.prototype.toArray = function() 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var myDate = this; 500)this.width=500'> var myArray = Array(); 500)this.width=500'> myArray[0] = myDate.getFullYear(); 500)this.width=500'> myArray[1] = myDate.getMonth(); 500)this.width=500'> myArray[2] = myDate.getDate(); 500)this.width=500'> myArray[3] = myDate.getHours(); 500)this.width=500'> myArray[4] = myDate.getMinutes(); 500)this.width=500'> myArray[5] = myDate.getSeconds(); 500)this.width=500'> return myArray; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 取得日期数据信息 500)this.width=500'>//| 参数 interval 表示数据类型 500)this.width=500'>//| y 年 m月 d日 w星期 ww周 h时 n分 s秒 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>Date.prototype.DatePart = function(interval) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var myDate = this; 500)this.width=500'> var partStr=''; 500)this.width=500'> var Week = ['日','一','二','三','四','五','六']; 500)this.width=500'> switch (interval) 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> case 'y' :partStr = myDate.getFullYear();break; 500)this.width=500'> case 'm' :partStr = myDate.getMonth()+1;break; 500)this.width=500'> case 'd' :partStr = myDate.getDate();break; 500)this.width=500'> case 'w' :partStr = Week[myDate.getDay()];break; 500)this.width=500'> case 'ww' :partStr = myDate.WeekNumOfYear();break; 500)this.width=500'> case 'h' :partStr = myDate.getHours();break; 500)this.width=500'> case 'n' :partStr = myDate.getMinutes();break; 500)this.width=500'> case 's' :partStr = myDate.getSeconds();break; 500)this.width=500'> } 500)this.width=500'> return partStr; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 取得当前日期所在月的最大天数 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>Date.prototype.MaxDayOfDate = function() 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var myDate = this; 500)this.width=500'> var ary = myDate.toArray(); 500)this.width=500'> var date1 = (new Date(ary[0],ary[1]+1,1)); 500)this.width=500'> var date2 = date1.dateAdd(1,'m',1); 500)this.width=500'> var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd')); 500)this.width=500'> return result; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 取得当前日期所在周是一年中的第几周 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>Date.prototype.WeekNumOfYear = function() 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> var myDate = this; 500)this.width=500'> var ary = myDate.toArray(); 500)this.width=500'> var year = ary[0]; 500)this.width=500'> var month = ary[1]+1; 500)this.width=500'> var day = ary[2]; 500)this.width=500'> document.write('< script language=VBScript\> \n'); 500)this.width=500'> document.write('myDate = DateValue(''+month+'-'+day+'-'+year+'') \n'); 500)this.width=500'> document.write('result = DatePart('ww', myDate) \n'); 500)this.width=500'> document.write(' \n'); 500)this.width=500'> return result; 500)this.width=500'>} 500)this.width=500'> 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>//| 字符串转成日期类型 500)this.width=500'>//| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd 500)this.width=500'>//+--------------------------------------------------- 500)this.width=500'>function StringToDate(DateStr) 500)this.width=500'>500)this.width=500'>500)this.width=500'>{ 500)this.width=500'> 500)this.width=500'> var converted = Date.parse(DateStr); 500)this.width=500'> var myDate = new Date(converted); 500)this.width=500'> if (isNaN(myDate)) 500)this.width=500'>500)this.width=500'> 500)this.width=500'>{ 500)this.width=500'> //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; 500)this.width=500'> var arys= DateStr.split('-'); 500)this.width=500'> myDate = new Date(arys[0],--arys[1],arys[2]); 500)this.width=500'> } 500)this.width=500'> return myDate; 500)this.width=500'>} |
|
|