1 var myDate = new Date();
2
3 myDate.getYear(); //获取当前年份(2位)
4
5 myDate.getFullYear(); //获取完整的年份(4位,1970-????)
6
7 myDate.getMonth(); //获取当前月份(0-11,0代表1月)
8
9 myDate.getDate(); //获取当前日(1-31)
10
11 myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
12
13 myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
14
15 myDate.getHours(); //获取当前小时数(0-23)
16
17 myDate.getMinutes(); //获取当前分钟数(0-59)
18
19 myDate.getSeconds(); //获取当前秒数(0-59)
20
21 myDate.getMilliseconds(); //获取当前毫秒数(0-999)
22
23 myDate.toLocaleDateString(); //获取当前日期
24
25 var mytime=myDate.toLocaleTimeString(); //获取当前时间
26
27 myDate.toLocaleString( ); //获取日期与时间
28
29 /*日期时间脚本库方法列表
30 Date.prototype.isLeapYear 判断闰年
31
32 Date.prototype.Format 日期格式化
33
34 Date.prototype.DateAdd 日期计算
35
36 Date.prototype.DateDiff 比较日期差
37
38 Date.prototype.toString 日期转字符串
39
40 Date.prototype.toArray 日期分割为数组
41
42 Date.prototype.DatePart 取日期的部分信息
43
44 Date.prototype.MaxDayOfDate 取日期所在月的最大天数
45
46 Date.prototype.WeekNumOfYear 判断日期所在年的第几周
47
48 StringToDate 字符串转日期型
49
50 IsValidDate 验证日期有效性
51
52 CheckDateTime 完整日期时间检查
53
54 daysBetween 日期天数差
55 js代码:*/
56 //---------------------------------------------------
57
58 // 判断闰年
59
60 //---------------------------------------------------
61
62 Date.prototype.isLeapYear = function()
63
64 {
65
66 return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
67
68 }
69
70
71 //---------------------------------------------------
72
73 // 日期格式化
74
75 // 格式 YYYY/yyyy/YY/yy 表示年份
76
77 // MM/M 月份
78
79 // W/w 星期
80
81 // dd/DD/d/D 日期
82
83 // hh/HH/h/H 时间
84
85 // mm/m 分钟
86
87 // ss/SS/s/S 秒
88
89 //---------------------------------------------------
90
91 Date.prototype.Format = function(formatStr)
92
93 {
94
95 var str = formatStr;
96
97 var Week = ['日','一','二','三','四','五','六'];
98
99
100 str=str.replace(/yyyy|YYYY/,this.getFullYear());
101
102 str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
103
104
105 str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
106
107 str=str.replace(/M/g,this.getMonth());
108
109
110 str=str.replace(/w|W/g,Week[this.getDay()]);
111
112
113 str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
114
115 str=str.replace(/d|D/g,this.getDate());
116
117
118 str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
119
120 str=str.replace(/h|H/g,this.getHours());
121
122 str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
123
124 str=str.replace(/m/g,this.getMinutes());
125
126
127 str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
128
129 str=str.replace(/s|S/g,this.getSeconds());
130
131
132 return str;
133
134 }
135
136
137 //+---------------------------------------------------
138
139 //| 求两个时间的天数差 日期格式为 YYYY-MM-dd
140
141 //+---------------------------------------------------
142
143 function daysBetween(DateOne,DateTwo)
144
145 {
146
147 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
148
149 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
150
151 var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
152
153
154 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
155
156 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
157
158 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
159
160
161 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
162
163 return Math.abs(cha);
164
165 }
166
167
168
169 //+---------------------------------------------------
170
171 //| 日期计算
172
173 //+---------------------------------------------------
174
175 Date.prototype.DateAdd = function(strInterval, Number) {
176
177 var dtTmp = this;
178
179 switch (strInterval) {
180
181 case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
182
183 case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
184
185 case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
186
187 case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
188
189 case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
190
191 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
192
193 case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
194
195 case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
196
197 }
198
199 }
200
201
202 //+---------------------------------------------------
203
204 //| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串
205
206 //+---------------------------------------------------
207
208 Date.prototype.DateDiff = function(strInterval, dtEnd) {
209
210 var dtStart = this;
211
212 if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
213
214 {
215
216 dtEnd = StringToDate(dtEnd);
217
218 }
219
220 switch (strInterval) {
221
222 case 's' :return parseInt((dtEnd - dtStart) / 1000);
223
224 case 'n' :return parseInt((dtEnd - dtStart) / 60000);
225
226 case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
227
228 case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
229
230 case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
231
232 case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
233
234 case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
235
236 }
237
238 }
239
240
241 //+---------------------------------------------------
242
243 //| 日期输出字符串,重载了系统的toString方法
244
245 //+---------------------------------------------------
246
247 Date.prototype.toString = function(showWeek)
248
249 {
250
251 var myDate= this;
252
253 var str = myDate.toLocaleDateString();
254
255 if (showWeek)
256
257 {
258
259 var Week = ['日','一','二','三','四','五','六'];
260
261 str += ' 星期' + Week[myDate.getDay()];
262
263 }
264
265 return str;
266
267 }
268
269
270 //+---------------------------------------------------
271
272 //| 日期合法性验证
273
274 //| 格式为:YYYY-MM-DD或YYYY/MM/DD
275
276 //+---------------------------------------------------
277
278 function IsValidDate(DateStr)
279
280 {
281
282 var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
283
284 if(sDate=='') return true;
285
286 //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
287
288 //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
289
290 var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
291
292 if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
293
294 {
295
296 var t=new Date(sDate.replace(/\-/g,'/'));
297
298 var ar = sDate.split(/[-/:]/);
299
300 if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
301
302 {
303
304 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
305
306 return false;
307
308 }
309
310 }
311
312 else
313
314 {
315
316 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
317
318 return false;
319
320 }
321
322 return true;
323
324 }
325
326
327 //+---------------------------------------------------
328
329 //| 日期时间检查
330
331 //| 格式为:YYYY-MM-DD HH:MM:SS
332
333 //+---------------------------------------------------
334
335 function CheckDateTime(str)
336
337 {
338
339 var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
340
341 var r = str.match(reg);
342
343 if(r==null)return false;
344
345 r[2]=r[2]-1;
346
347 var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
348
349 if(d.getFullYear()!=r[1])return false;
350
351 if(d.getMonth()!=r[2])return false;
352
353 if(d.getDate()!=r[3])return false;
354
355 if(d.getHours()!=r[4])return false;
356
357 if(d.getMinutes()!=r[5])return false;
358
359 if(d.getSeconds()!=r[6])return false;
360
361 return true;
362
363 }
364
365
366 //+---------------------------------------------------
367
368 //| 把日期分割成数组
369
370 //+---------------------------------------------------
371
372 Date.prototype.toArray = function()
373
374 {
375
376 var myDate = this;
377
378 var myArray = Array();
379
380 myArray[0] = myDate.getFullYear();
381
382 myArray[1] = myDate.getMonth();
383
384 myArray[2] = myDate.getDate();
385
386 myArray[3] = myDate.getHours();
387
388 myArray[4] = myDate.getMinutes();
389
390 myArray[5] = myDate.getSeconds();
391
392 return myArray;
393
394 }
395
396
397 //+---------------------------------------------------
398
399 //| 取得日期数据信息
400
401 //| 参数 interval 表示数据类型
402
403 //| y 年 m月 d日 w星期 ww周 h时 n分 s秒
404
405 //+---------------------------------------------------
406
407 Date.prototype.DatePart = function(interval)
408
409 {
410
411 var myDate = this;
412
413 var partStr='';
414
415 var Week = ['日','一','二','三','四','五','六'];
416
417 switch (interval)
418
419 {
420
421 case 'y' :partStr = myDate.getFullYear();break;
422
423 case 'm' :partStr = myDate.getMonth()+1;break;
424
425 case 'd' :partStr = myDate.getDate();break;
426
427 case 'w' :partStr = Week[myDate.getDay()];break;
428
429 case 'ww' :partStr = myDate.WeekNumOfYear();break;
430
431 case 'h' :partStr = myDate.getHours();break;
432
433 case 'n' :partStr = myDate.getMinutes();break;
434
435 case 's' :partStr = myDate.getSeconds();break;
436
437 }
438
439 return partStr;
440
441 }
442
443
444 //+---------------------------------------------------
445
446 //| 取得当前日期所在月的最大天数
447
448 //+---------------------------------------------------
449
450 Date.prototype.MaxDayOfDate = function()
451
452 {
453
454 var myDate = this;
455
456 var ary = myDate.toArray();
457
458 var date1 = (new Date(ary[0],ary[1]+1,1));
459
460 var date2 = date1.dateAdd(1,'m',1);
461
462 var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
463
464 return result;
465
466 }
467
468
469 //+---------------------------------------------------
470
471 //| 取得当前日期所在周是一年中的第几周
472
473 //+---------------------------------------------------
474
475 Date.prototype.WeekNumOfYear = function()
476
477 {
478
479 var myDate = this;
480
481 var ary = myDate.toArray();
482
483 var year = ary[0];
484
485 var month = ary[1]+1;
486
487 var day = ary[2];
488
489 document.write('< script language=VBScript\> \n');
490
491 document.write('myDate = Datue(''+month+'-'+day+'-'+year+'') \n');
492
493 document.write('result = DatePart('ww', myDate) \n');
494
495 document.write(' \n');
496
497 return result;
498
499 }
500
501
502 //+---------------------------------------------------
503
504 //| 字符串转成日期类型
505
506 //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
507
508 //+---------------------------------------------------
509
510 function StringToDate(DateStr)
511
512 {
513
514
515 var converted = Date.parse(DateStr);
516
517 var myDate = new Date(converted);
518
519 if (isNaN(myDate))
520
521 {
522
523 //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
524
525 var arys= DateStr.split('-');
526
527 myDate = new Date(arys[0],--arys[1],arys[2]);
528
529 }
530
531 return myDate;
532
533 }
534
535 若要显示:当前日期加时间(如:2009-06-12 12:00)
536 function CurentTime()
537
538 {
539
540 var now = new Date();
541
542
543 var year = now.getFullYear(); //年
544
545 var month = now.getMonth() + 1; //月
546
547 var day = now.getDate(); //日
548
549
550 var hh = now.getHours(); //时
551
552 var mm = now.getMinutes(); //分
553
554
555 var clock = year + "-";
556
557
558 if(month < 10)
559
560 clock += "0";
561
562
563 clock += month + "-";
564
565
566 if(day < 10)
567
568 clock += "0";
569
570
571 clock += day + " ";
572
573
574 if(hh < 10)
575
576 clock += "0";
577
578
579 clock += hh + ":";
580
581 if (mm < 10) clock += '0';
582
583 clock += mm;
584
585 return(clock);
586
587 }
588
589
590
591
592
593
594
595
596
597
598 ;(function(window){
599
600 /**
601
602 * [dateDiff 算时间差]
603
604 * @param {[type=Number]} hisTime [历史时间戳,必传]
605
606 * @param {[type=Number]} nowTime [当前时间戳,不传将获取当前时间戳]
607
608 * @return {[string]} [string]
609
610 */
611
612 var dateDiff = function(hisTime,nowTime){
613
614 if(!arguments.length) return '';
615
616 var arg = arguments,
617
618 now =arg[1]?arg[1]:new Date().getTime(),
619
620 diffValue = now - arg[0],
621
622 result='',
623
624
625
626 minute = 1000 * 60,
627
628 hour = minute * 60,
629
630 day = hour * 24,
631
632 halfamonth = day * 15,
633
634 month = day * 30,
635
636 year = month * 12,
637
638
639
640 _year = diffValue/year,
641
642 _month =diffValue/month,
643
644 _week =diffValue/(7*day),
645
646 _day =diffValue/day,
647
648 _hour =diffValue/hour,
649
650 _min =diffValue/minute;
651
652 if(_year>=1) result=parseInt(_year) + "年前";
653 else if(_month>=1) result=parseInt(_month) + "个月前";
654 else if(_week>=1) result=parseInt(_week) + "周前";
655 else if(_day>=1) result=parseInt(_day) +"天前";
656 else if(_hour>=1) result=parseInt(_hour) +"个小时前";
657 else if(_min>=1) result=parseInt(_min) +"分钟前";
658 else result="刚刚";
659 return result;
660 }
661 window.dateDiff = dateDiff
662 })(window);
663
664
665
666
667