《日期时间脚本库方法列表》由会员分享,可在线阅读,更多相关《日期时间脚本库方法列表(5页珍藏版)》请在金锄头文库上搜索。
1、1. 日期时间脚本库方法列表 2. Date.prototype.isLeapYear 判断闰年 3. Date.prototype.Format 日期格式化 4. Date.prototype.DateAdd 日期计算 5. Date.prototype.DateDiff 比较日期差 6. Date.prototype.toString 日期转字符串 7. Date.prototype.toArray 日期分割为数组 8. Date.prototype.DatePart 取日期的部分信息 9. Date.prototype.MaxDayOfDate 取日期所在月的最大天数 10.Date.p
2、rototype.WeekNumOfYear 判断日期所在年的第几周 11.StringToDate 字符串转日期型 12.IsValidDate 验证日期有效性 13.CheckDateTime 完整日期时间检查 14.daysBetween 日期天数差 15. 16.js 代码 17. 18./- 19./ 判断闰年 20./- 21.Date.prototype.isLeapYear = function() 22. 23.return (0=this.getYear()%4 24. 25. 26./- 27./ 日期格式化 28./ 格式 YYYY/yyyy/YY/yy 表示年份 29
3、./ MM/M 月份 30./ W/w 星期 31./ dd/DD/d/D 日期 32./ hh/HH/h/H 时间 33./ mm/m 分钟 34./ ss/SS/s/S 秒 35./- 36.Date.prototype.Format = function(formatStr) 37. 38.var str = formatStr; 39.var Week = 日,一,二,三,四,五,六; 40. 41. str=str.replace(/yyyy|YYYY/,this.getFullYear(); 42. str=str.replace(/yy|YY/,(this.getYear() %
4、 100)9?(this.get Year() % 100).toString():0 + (this.getYear() % 100); 43. 44. str=str.replace(/MM/,this.getMonth()9?this.getMonth().toS tring():0 + this.getMonth(); 45. str=str.replace(/M/g,this.getMonth(); 46. 47. str=str.replace(/w|W/g,Weekthis.getDay(); 48. 49. str=str.replace(/dd|DD/,this.getDat
5、e()9?this.getDate().to String():0 + this.getDate(); 50. str=str.replace(/d|D/g,this.getDate(); 51. 52. str=str.replace(/hh|HH/,this.getHours()9?this.getHours(). toString():0 + this.getHours(); 53. str=str.replace(/h|H/g,this.getHours(); 54. str=str.replace(/mm/,this.getMinutes()9?this.getMinutes().t
6、 oString():0 + this.getMinutes(); 55. str=str.replace(/m/g,this.getMinutes(); 56. 57. str=str.replace(/ss|SS/,this.getSeconds()9?this.getSecond s().toString():0 + this.getSeconds(); 58. str=str.replace(/s|S/g,this.getSeconds(); 59. 60.return str; 61. 62. 63./+- 64./| 求两个时间的天数差 日期格式为 YYYY-MM-dd 65./+
7、- 66.function daysBetween(DateOne,DateTwo) 67. 68.var OneMonth = DateOne.substring(5,DateOne.lastIndexOf (- ); 69.var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndex Of (-)+1); 70.var OneYear = DateOne.substring(0,DateOne.indexOf (-); 71. 72.var TwoMonth = DateTwo.substring(5,DateTwo.las
8、tIndexOf (- ); 73.var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndex Of (-)+1); 74.var TwoYear = DateTwo.substring(0,DateTwo.indexOf (-); 75. 76.var cha=(Date.parse(OneMonth+/+OneDay+/+OneYear)-Date.parse(TwoMonth+/+TwoDay+/+TwoYear)/86400000); 77.return Math.abs(cha); 78. 79. 80. 81./+
9、- 82./| 日期计算 83./+- 84.Date.prototype.DateAdd = function(strInterval, Number) 85.var dtTmp = this; 86.switch (strInterval) 87.case s :return new Date(Date.parse(dtTmp) + (1000 * Number);88.case n :return new Date(Date.parse(dtTmp) + (60000 * Number); 89.case h :return new Date(Date.parse(dtTmp) + (3
10、600000 * Number) ; 90.case d :return new Date(Date.parse(dtTmp) + (86400000 * Numbe r); 91.case w :return new Date(Date.parse(dtTmp) + (86400000 * 7) *Number); 92.case q :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()+ Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinute s(), dtTmp.g
11、etSeconds(); 93.case m :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()+ Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),dtTmp.getSeconds(); 94.case y :return new Date(dtTmp.getFullYear() + Number), dtTmp.g etMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),dtTmp.g
12、etSeconds(); 95. 96. 97. 98./+- 99./| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串 100./+- 101.Date.prototype.DateDiff = function(strInterval, dtEnd) 102.var dtStart = this; 103.if (typeof dtEnd = string )/如果是字符串转换为日期型 104. 105. dtEnd = StringToDate(dtEnd); 106. 107.switch (strInterval) 108.case s :return parseInt(dtEnd - dtStart) / 1000); 109.case n :return parseInt(dtEnd - dtStart) / 60000); 110.case h :return parseInt(dtEnd - dtStart) / 3600000); 111.case d :return pa