在JavaScript中,时间处理是经常需要用到的。最近想要慢慢建立自己的代码库,整理了几个之前用到的js处理时间的函数,发出来跟大家分享一下,以后的使用中会不断增加和修改代码库。
把字符串转换为日期对象
有时需要把字符串转换为日期对象,但是IE下不支持new Date("2011-04-07")这样实例化日期对象,ff下支持,所以写了一个扩展函数,用来把形如yyyy-mm-dd或者dd/mm/yyyy的字符串转换为日期对象。代码如下:
view sourceprint?
01 /*
02 函数:把字符串转换为日期对象
03 参数:yyyy-mm-dd或dd/mm/yyyy形式的字符串
04 返回:Date对象
05 注:IE下不支持直接实例化日期对象,如new Date("2011-04-06")
06 */
07 Date.prototype.convertDate = function (date)
08 {
09 var flag = true;
10 var dateArray = date.split("-");
11 if (dateArray.length != 3)
12 {
13 dateArray = date.split("/");
14 if (dateArray.length != 3)
15 {
16 return null;
17 }
18 flag = false;
19 }
20 var newDate = new Date();
21 if (flag)
22 {
23 // month从0开始
24 newDate.setFullYear(dateArray[0], dateArray[1] - 1, dateArray[2]);
25 }
26 else
27 {
28 newDate.setFullYear(dateArray[2], dateArray[1] - 1, dateArray[0]);
29 }
30 newDate.setHours(0, 0, 0);
31 return newDate;
32 };
测试:
view sourceprint?
1 <script type="text/javascript">
2 document.writeln(new Date().convertDate("7/12/2011"));
3 </script>
输出:Wed Dec 07 2011 17:30:58 GMT+0800
计算两个日期之间的差值
这个函数可以计算两个日期之前的差值(毫秒/秒/分/小时/天),主要用到了getTime()函数和Math.floor()函数,代码如下:
view sourceprint?
01 /*
02 函数:计算两个日期之间的差值
03 参数:date是日期对象
04 flag:ms-毫秒,s-秒,m-分,h-小时,d-天,M-月,y-年
05 返回:当前日期和date两个日期相差的毫秒/秒/分/小时/天
06 */
07 Date.prototype.dateDiff = function (date, flag)
08 {
09 var msCount;
10 var diff = this.getTime() - date.getTime();
11 switch (flag)
12 {
13 case "ms":
14 msCount = 1;
15 break;
16 case "s":
17 msCount = 1000;
18 break;
19 case "m":
20 msCount = 60 * 1000;
21 break;
22 case "h":
23 msCount = 60 * 60 * 1000;
24 break;
25 case "d":
26 msCount = 24 * 60 * 60 * 1000;
27 break;
28 }
29 return Math.floor(diff / msCount);
30 };
测试:
view sourceprint?
1 <script type="text/javascript">
2 var d1 = new Date().convertDate("2011-04-08");
3 var d2 = new Date().convertDate("2011-04-07");
4 document.writeln(d1.dateDiff(d2, 'd'));
5 </script>
判断一个年份是否为闰年
view sourceprint?
1 /*
2 函数:判断一个年份是否为闰年
3 返回:是否为闰年
4 */
5 Date.prototype.isLeapYear = function ()
6 {
7 var year = this.getFullYear();
8 return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
9 }
测试:
view sourceprint?
1 <script type="text/javascript">
2 document.writeln(new Date().convertDate("2000-04-08").isLeapYear() + "<BR>");
3 document.writeln(new Date().convertDate("2011-04-08").isLeapYear() + "<BR>");
4 </script>
5 // 结果:
6 // true
7 // false
格式化日期
这个函数是从网上搜来的,稍微修改了一下,它类似.net中Format函数,传入格式化字符串如"yyyy-MM-dd",返回格式化后的日期。
view sourceprint?
01 /*
02 函数:格式化日期
03 参数:formatStr-格式化字符串
04 d:将日显示为不带前导零的数字,如1
05 dd:将日显示为带前导零的数字,如01
06 ddd:将日显示为缩写形式,如Sun
07 dddd:将日显示为全名,如Sunday
08 M:将月份显示为不带前导零的数字,如一月显示为1
09 MM:将月份显示为带前导零的数字,如01
10 MMM:将月份显示为缩写形式,如Jan
11 MMMM:将月份显示为完整月份名,如January
12 yy:以两位数字格式显示年份
13 yyyy:以四位数字格式显示年份
14 h:使用12小时制将小时显示为不带前导零的数字,注意||的用法
15 hh:使用12小时制将小时显示为带前导零的数字
16 H:使用24小时制将小时显示为不带前导零的数字
17 HH:使用24小时制将小时显示为带前导零的数字
18 m:将分钟显示为不带前导零的数字
19 mm:将分钟显示为带前导零的数字
20 s:将秒显示为不带前导零的数字
21 ss:将秒显示为带前导零的数字
22 l:将毫秒显示为不带前导零的数字
23 ll:将毫秒显示为带前导零的数字
24 tt:显示am/pm
25 TT:显示AM/PM
26 返回:格式化后的日期
27 */
28 Date.prototype.format = function (formatStr)
29 {
30 var date = this;
31
32 /*
33 函数:填充0字符
34 参数:value-需要填充的字符串, length-总长度
35 返回:填充后的字符串
36 */
37 var zeroize = function (value, length)
38 {
39 if (!length)
40 {
41 length = 2;
42 }
43 value = new String(value);
44 for (var i = 0, zeros = ''; i < (length - value.length); i++)
45 {
46 zeros += '0';
47 }
48 return zeros + value;
49 };
50
51 return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function($0)
52 {
53 switch ($0)
54 {
55 case 'd': return date.getDate();
56 case 'dd': return zeroize(date.getDate());
57 case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];
58 case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
59 case 'M': return date.getMonth() + 1;
60 case 'MM': return zeroize(date.getMonth() + 1);
61 case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];
62 case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];
63 case 'yy': return new String(date.getFullYear()).substr(2);
64 case 'yyyy': return date.getFullYear();
65 case 'h': return date.getHours() % 12 || 12;
66 case 'hh': return zeroize(date.getHours() % 12 || 12);
67 case 'H': return date.getHours();
68 case 'HH': return zeroize(date.getHours());
69 case 'm': return date.getMinutes();
70 case 'mm': return zeroize(date.getMinutes());
71 case 's': return date.getSeconds();
72 case 'ss': return zeroize(date.getSeconds());
73 case 'l': return date.getMilliseconds();
74 case 'll': return zeroize(date.getMilliseconds());
75 case 'tt': return date.getHours() < 12 ? 'am' : 'pm';
76 case 'TT': return date.getHours() < 12 ? 'AM' : 'PM';
77 }
78 });
79 }
测试:
view sourceprint?1 <script type="text/javascript">
2 document.writeln(new Date().format("yyyy-MM-dd hh:mm:ss"));
3 </script>
4 // 结果:
5 // 2011-04-08 10:13:37
一些JavaScript时间处理函数整理
最新推荐文章于 2024-09-20 11:38:47 发布