js日期时间函数(经典+完善+实用)
日期时间脚本库方法列表
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 日期天数差
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 代码
1 //---------------------------------------------------
2 // 判断闰年
3 //---------------------------------------------------
4 Date.prototype.isLeapYear = function()
5 {
6 return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
7 }
8
9 //---------------------------------------------------
10 // 日期格式化
11 // 格式 YYYY/yyyy/YY/yy 表示年份
12 // MM/M 月份
13 // W/w 星期
14 // dd/DD/d/D 日期
15 // hh/HH/h/H 时间
16 // mm/m 分钟
17 // ss/SS/s/S 秒
18 //---------------------------------------------------
19 Date.prototype.Format = function(formatStr)
20 {
21 var str = formatStr;
22 var Week = ['日','一','二','三','四','五','六'];
23
24 str=str.replace(/yyyy|YYYY/,this.getFullYear());
25 str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
26
27 str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
28 str=str.replace(/M/g,this.getMonth());
29
30 str=str.replace(/w|W/g,Week[this.getDay()]);
31
32 str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
33 str=str.replace(/d|D/g,this.getDate());
34
35 str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
36 str=str.replace(/h|H/g,this.getHours());
37 str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
38 str=str.replace(/m/g,this.getMinutes());
39
40 str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
41 str=str.replace(/s|S/g,this.getSeconds());
42
43 return str;
44 }
45
46 //+---------------------------------------------------
47 //| 求两个时间的天数差 日期格式为 YYYY-MM-dd
48 //+---------------------------------------------------
49 function daysBetween(DateOne,DateTwo)
50 {
51 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
52 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
53 var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
54
55 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
56 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
57 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
58
59 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
60 return Math.abs(cha);
61 }
62
63
64 //+---------------------------------------------------
65 //| 日期计算
66 //+---------------------------------------------------
67 Date.prototype.DateAdd = function(strInterval, Number) {
68 var dtTmp = this;
69 switch (strInterval) {
70 case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
71 case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
72 case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
73 case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
74 case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
75 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
76 case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
77 case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
78 }
79 }
80
81 //+---------------------------------------------------
82 //| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
83 //+---------------------------------------------------
84 Date.prototype.DateDiff = function(strInterval, dtEnd) {
85 var dtStart = this;
86 if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
87 {
88 dtEnd = StringToDate(dtEnd);
89 }
90 switch (strInterval) {
91 case 's' :return parseInt((dtEnd - dtStart) / 1000);
92 case 'n' :return parseInt((dtEnd - dtStart) / 60000);
93 case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
94 case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
95 case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
96 case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
97 case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
98 }
99 }
100
101 //+---------------------------------------------------
102 //| 日期输出字符串,重载了系统的toString方法
103 //+---------------------------------------------------
104 Date.prototype.toString = function(showWeek)
105 {
106 var myDate= this;
107 var str = myDate.toLocaleDateString();
108 if (showWeek)
109 {
110 var Week = ['日','一','二','三','四','五','六'];
111 str += ' 星期' + Week[myDate.getDay()];
112 }
113 return str;
114 }
115
116 //+---------------------------------------------------
117 //| 日期合法性验证
118 //| 格式为:YYYY-MM-DD或YYYY/MM/DD
119 //+---------------------------------------------------
120 function IsValidDate(DateStr)
121 {
122 var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
123 if(sDate=='') return true;
124 //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
125 //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
126 var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
127 if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
128 {
129 var t=new Date(sDate.replace(/\-/g,'/'));
130 var ar = sDate.split(/[-/:]/);
131 if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
132 {
133 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
134 return false;
135 }
136 }
137 else
138 {
139 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
140 return false;
141 }
142 return true;
143 }
144
145 //+---------------------------------------------------
146 //| 日期时间检查
147 //| 格式为:YYYY-MM-DD HH:MM:SS
148 //+---------------------------------------------------
149 function CheckDateTime(str)
150 {
151 var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
152 var r = str.match(reg);
153 if(r==null)return false;
154 r[2]=r[2]-1;
155 var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
156 if(d.getFullYear()!=r[1])return false;
157 if(d.getMonth()!=r[2])return false;
158 if(d.getDate()!=r[3])return false;
159 if(d.getHours()!=r[4])return false;
160 if(d.getMinutes()!=r[5])return false;
161 if(d.getSeconds()!=r[6])return false;
162 return true;
163 }
164
165 //+---------------------------------------------------
166 //| 把日期分割成数组
167 //+---------------------------------------------------
168 Date.prototype.toArray = function()
169 {
170 var myDate = this;
171 var myArray = Array();
172 myArray[0] = myDate.getFullYear();
173 myArray[1] = myDate.getMonth();
174 myArray[2] = myDate.getDate();
175 myArray[3] = myDate.getHours();
176 myArray[4] = myDate.getMinutes();
177 myArray[5] = myDate.getSeconds();
178 return myArray;
179 }
180
181 //+---------------------------------------------------
182 //| 取得日期数据信息
183 //| 参数 interval 表示数据类型
184 //| y 年 m月 d日 w星期 ww周 h时 n分 s秒
185 //+---------------------------------------------------
186 Date.prototype.DatePart = function(interval)
187 {
188 var myDate = this;
189 var partStr='';
190 var Week = ['日','一','二','三','四','五','六'];
191 switch (interval)
192 {
193 case 'y' :partStr = myDate.getFullYear();break;
194 case 'm' :partStr = myDate.getMonth()+1;break;
195 case 'd' :partStr = myDate.getDate();break;
196 case 'w' :partStr = Week[myDate.getDay()];break;
197 case 'ww' :partStr = myDate.WeekNumOfYear();break;
198 case 'h' :partStr = myDate.getHours();break;
199 case 'n' :partStr = myDate.getMinutes();break;
200 case 's' :partStr = myDate.getSeconds();break;
201 }
202 return partStr;
203 }
204
205 //+---------------------------------------------------
206 //| 取得当前日期所在月的最大天数
207 //+---------------------------------------------------
208 Date.prototype.MaxDayOfDate = function()
209 {
210 var myDate = this;
211 var ary = myDate.toArray();
212 var date1 = (new Date(ary[0],ary[1]+1,1));
213 var date2 = date1.dateAdd(1,'m',1);
214 var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
215 return result;
216 }
217
218 //+---------------------------------------------------
219 //| 取得当前日期所在周是一年中的第几周
220 //+---------------------------------------------------
221 Date.prototype.WeekNumOfYear = function()
222 {
223 var myDate = this;
224 var ary = myDate.toArray();
225 var year = ary[0];
226 var month = ary[1]+1;
227 var day = ary[2];
228 document.write('< script language=VBScript\> \n');
229 document.write('myDate = DateValue(''+month+'-'+day+'-'+year+'') \n');
230 document.write('result = DatePart('ww', myDate) \n');
231 document.write(' \n');
232 return result;
233 }
234
235 //+---------------------------------------------------
236 //| 字符串转成日期类型
237 //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
238 //+---------------------------------------------------
239 function StringToDate(DateStr)
240 {
241
242 var converted = Date.parse(DateStr);
243 var myDate = new Date(converted);
244 if (isNaN(myDate))
245 {
246 //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
247 var arys= DateStr.split('-');
248 myDate = new Date(arys[0],--arys[1],arys[2]);
249 }
250 return myDate;
251 }
252
2 // 判断闰年
3 //---------------------------------------------------
4 Date.prototype.isLeapYear = function()
5 {
6 return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
7 }
8
9 //---------------------------------------------------
10 // 日期格式化
11 // 格式 YYYY/yyyy/YY/yy 表示年份
12 // MM/M 月份
13 // W/w 星期
14 // dd/DD/d/D 日期
15 // hh/HH/h/H 时间
16 // mm/m 分钟
17 // ss/SS/s/S 秒
18 //---------------------------------------------------
19 Date.prototype.Format = function(formatStr)
20 {
21 var str = formatStr;
22 var Week = ['日','一','二','三','四','五','六'];
23
24 str=str.replace(/yyyy|YYYY/,this.getFullYear());
25 str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
26
27 str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
28 str=str.replace(/M/g,this.getMonth());
29
30 str=str.replace(/w|W/g,Week[this.getDay()]);
31
32 str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
33 str=str.replace(/d|D/g,this.getDate());
34
35 str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
36 str=str.replace(/h|H/g,this.getHours());
37 str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
38 str=str.replace(/m/g,this.getMinutes());
39
40 str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
41 str=str.replace(/s|S/g,this.getSeconds());
42
43 return str;
44 }
45
46 //+---------------------------------------------------
47 //| 求两个时间的天数差 日期格式为 YYYY-MM-dd
48 //+---------------------------------------------------
49 function daysBetween(DateOne,DateTwo)
50 {
51 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
52 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
53 var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
54
55 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
56 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
57 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
58
59 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
60 return Math.abs(cha);
61 }
62
63
64 //+---------------------------------------------------
65 //| 日期计算
66 //+---------------------------------------------------
67 Date.prototype.DateAdd = function(strInterval, Number) {
68 var dtTmp = this;
69 switch (strInterval) {
70 case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
71 case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
72 case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
73 case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
74 case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
75 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
76 case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
77 case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
78 }
79 }
80
81 //+---------------------------------------------------
82 //| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
83 //+---------------------------------------------------
84 Date.prototype.DateDiff = function(strInterval, dtEnd) {
85 var dtStart = this;
86 if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
87 {
88 dtEnd = StringToDate(dtEnd);
89 }
90 switch (strInterval) {
91 case 's' :return parseInt((dtEnd - dtStart) / 1000);
92 case 'n' :return parseInt((dtEnd - dtStart) / 60000);
93 case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
94 case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
95 case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
96 case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
97 case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
98 }
99 }
100
101 //+---------------------------------------------------
102 //| 日期输出字符串,重载了系统的toString方法
103 //+---------------------------------------------------
104 Date.prototype.toString = function(showWeek)
105 {
106 var myDate= this;
107 var str = myDate.toLocaleDateString();
108 if (showWeek)
109 {
110 var Week = ['日','一','二','三','四','五','六'];
111 str += ' 星期' + Week[myDate.getDay()];
112 }
113 return str;
114 }
115
116 //+---------------------------------------------------
117 //| 日期合法性验证
118 //| 格式为:YYYY-MM-DD或YYYY/MM/DD
119 //+---------------------------------------------------
120 function IsValidDate(DateStr)
121 {
122 var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
123 if(sDate=='') return true;
124 //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
125 //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
126 var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
127 if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
128 {
129 var t=new Date(sDate.replace(/\-/g,'/'));
130 var ar = sDate.split(/[-/:]/);
131 if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
132 {
133 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
134 return false;
135 }
136 }
137 else
138 {
139 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
140 return false;
141 }
142 return true;
143 }
144
145 //+---------------------------------------------------
146 //| 日期时间检查
147 //| 格式为:YYYY-MM-DD HH:MM:SS
148 //+---------------------------------------------------
149 function CheckDateTime(str)
150 {
151 var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
152 var r = str.match(reg);
153 if(r==null)return false;
154 r[2]=r[2]-1;
155 var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
156 if(d.getFullYear()!=r[1])return false;
157 if(d.getMonth()!=r[2])return false;
158 if(d.getDate()!=r[3])return false;
159 if(d.getHours()!=r[4])return false;
160 if(d.getMinutes()!=r[5])return false;
161 if(d.getSeconds()!=r[6])return false;
162 return true;
163 }
164
165 //+---------------------------------------------------
166 //| 把日期分割成数组
167 //+---------------------------------------------------
168 Date.prototype.toArray = function()
169 {
170 var myDate = this;
171 var myArray = Array();
172 myArray[0] = myDate.getFullYear();
173 myArray[1] = myDate.getMonth();
174 myArray[2] = myDate.getDate();
175 myArray[3] = myDate.getHours();
176 myArray[4] = myDate.getMinutes();
177 myArray[5] = myDate.getSeconds();
178 return myArray;
179 }
180
181 //+---------------------------------------------------
182 //| 取得日期数据信息
183 //| 参数 interval 表示数据类型
184 //| y 年 m月 d日 w星期 ww周 h时 n分 s秒
185 //+---------------------------------------------------
186 Date.prototype.DatePart = function(interval)
187 {
188 var myDate = this;
189 var partStr='';
190 var Week = ['日','一','二','三','四','五','六'];
191 switch (interval)
192 {
193 case 'y' :partStr = myDate.getFullYear();break;
194 case 'm' :partStr = myDate.getMonth()+1;break;
195 case 'd' :partStr = myDate.getDate();break;
196 case 'w' :partStr = Week[myDate.getDay()];break;
197 case 'ww' :partStr = myDate.WeekNumOfYear();break;
198 case 'h' :partStr = myDate.getHours();break;
199 case 'n' :partStr = myDate.getMinutes();break;
200 case 's' :partStr = myDate.getSeconds();break;
201 }
202 return partStr;
203 }
204
205 //+---------------------------------------------------
206 //| 取得当前日期所在月的最大天数
207 //+---------------------------------------------------
208 Date.prototype.MaxDayOfDate = function()
209 {
210 var myDate = this;
211 var ary = myDate.toArray();
212 var date1 = (new Date(ary[0],ary[1]+1,1));
213 var date2 = date1.dateAdd(1,'m',1);
214 var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
215 return result;
216 }
217
218 //+---------------------------------------------------
219 //| 取得当前日期所在周是一年中的第几周
220 //+---------------------------------------------------
221 Date.prototype.WeekNumOfYear = function()
222 {
223 var myDate = this;
224 var ary = myDate.toArray();
225 var year = ary[0];
226 var month = ary[1]+1;
227 var day = ary[2];
228 document.write('< script language=VBScript\> \n');
229 document.write('myDate = DateValue(''+month+'-'+day+'-'+year+'') \n');
230 document.write('result = DatePart('ww', myDate) \n');
231 document.write(' \n');
232 return result;
233 }
234
235 //+---------------------------------------------------
236 //| 字符串转成日期类型
237 //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
238 //+---------------------------------------------------
239 function StringToDate(DateStr)
240 {
241
242 var converted = Date.parse(DateStr);
243 var myDate = new Date(converted);
244 if (isNaN(myDate))
245 {
246 //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
247 var arys= DateStr.split('-');
248 myDate = new Date(arys[0],--arys[1],arys[2]);
249 }
250 return myDate;
251 }
252