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