001 /*
002 * Date Format 1.2.3
003 * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
004 * MIT license
005 *
006 * Includes enhancements by Scott Trenda <scott.trenda.net>
007 * and Kris Kowal <cixar.com/~kris.kowal/>
008 *
009 * Accepts a date, a mask, or a date and a mask.
010 * Returns a formatted version of the given date.
011 * The date defaults to the current date/time.
012 * The mask defaults to dateFormat.masks.default.
013 */
014
015 var dateFormat = function () {
016 var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
017 timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
018 timezoneClip = /[^-+\dA-Z]/g,
019 pad = function (val, len) {
020 val = String(val);
021 len = len || 2;
022 while (val.length < len) val = "0" + val;
023 return val;
024 };
025
026 // Regexes and supporting functions are cached through closure
027 return function (date, mask, utc) {
028 var dF = dateFormat;
029
030 // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
031 if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
032 mask = date;
033 date = undefined;
034 }
035
036 // Passing date through Date applies Date.parse, if necessary
037 date = date ? new Date(date) : new Date;
038 if (isNaN(date)) throw SyntaxError("invalid date");
039
040 mask = String(dF.masks[mask] || mask || dF.masks["default"]);
041
042 // Allow setting the utc argument via the mask
043 if (mask.slice(0, 4) == "UTC:") {
044 mask = mask.slice(4);
045 utc = true;
046 }
047
048 var _ = utc ? "getUTC" : "get",
049 d = date[_ + "Date"](),
050 D = date[_ + "Day"](),
051 m = date[_ + "Month"](),
052 y = date[_ + "FullYear"](),
053 H = date[_ + "Hours"](),
054 M = date[_ + "Minutes"](),
055 s = date[_ + "Seconds"](),
056 L = date[_ + "Milliseconds"](),
057 o = utc ? 0 : date.getTimezoneOffset(),
058 flags = {
059 d: d,
060 dd: pad(d),
061 ddd: dF.i18n.dayNames[D],
062 dddd: dF.i18n.dayNames[D + 7],
063 m: m + 1,
064 mm: pad(m + 1),
065 mmm: dF.i18n.monthNames[m],
066 mmmm: dF.i18n.monthNames[m + 12],
067 yy: String(y).slice(2),
068 yyyy: y,
069 h: H % 12 || 12,
070 hh: pad(H % 12 || 12),
071 H: H,
072 HH: pad(H),
073 M: M,
074 MM: pad(M),
075 s: s,
076 ss: pad(s),
077 l: pad(L, 3),
078 L: pad(L > 99 ? Math.round(L / 10) : L),
079 t: H < 12 ? "a" : "p",
080 tt: H < 12 ? "am" : "pm",
081 T: H < 12 ? "A" : "P",
082 TT: H < 12 ? "AM" : "PM",
083 Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
084 o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
085 S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
086 };
087
088 return mask.replace(token, function ($0) {
089 return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
090 });
091 };
092 }();
093
094 // Some common format strings
095 dateFormat.masks = {
096 "default": "ddd mmm dd yyyy HH:MM:ss",
097 shortDate: "m/d/yy",
098 mediumDate: "mmm d, yyyy",
099 longDate: "mmmm d, yyyy",
100 fullDate: "dddd, mmmm d, yyyy",
101 shortTime: "h:MM TT",
102 mediumTime: "h:MM:ss TT",
103 longTime: "h:MM:ss TT Z",
104 isoDate: "yyyy-mm-dd",
105 isoTime: "HH:MM:ss",
106 isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
107 isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
108 };
109
110 // Internationalization strings
111 dateFormat.i18n = {
112 dayNames: [
113 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
114 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
115 ],
116 monthNames: [
117 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
118 "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
119 ]
120 };
121
122 // For convenience...
123 Date.prototype.format = function (mask, utc) {
124 return dateFormat(this, mask, utc);
125 };
[代码] 示例代码
01 var now = new Date();
02
03 now.format("m/dd/yy");
04 // Returns, e.g., 6/09/07
05
06 // Can also be used as a standalone function
07 dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
08 // Saturday, June 9th, 2007, 5:46:21 PM
09
10 // You can use one of several named masks
11 now.format("isoDateTime");
12 // 2007-06-09T17:46:21
13
14 // ...Or add your own
15 dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
16 now.format("hammerTime");
17 // 17:46! Can't touch this!
18
19 // When using the standalone dateFormat function,
20 // you can also provide the date as a string
21 dateFormat("Jun 9 2007", "fullDate");
22 // Saturday, June 9, 2007
23
24 // Note that if you don't include the mask argument,
25 // dateFormat.masks.default is used
26 now.format();
27 // Sat Jun 09 2007 17:46:21
28
29 // And if you don't include the date argument,
30 // the current date and time is used
31 dateFormat();
32 // Sat Jun 09 2007 17:46:22
33
34 // You can also skip the date argument (as long as your mask doesn't
35 // contain any numbers), in which case the current date/time is used
36 dateFormat("longTime");
37 // 5:46:22 PM EST
38
39 // And finally, you can convert local time to UTC time. Either pass in
40 // true as an additional argument (no argument skipping allowed in this case):
41 dateFormat(now, "longTime", true);
42 now.format("longTime", true);
43 // Both lines return, e.g., 10:46:21 PM UTC
44
45 // ...Or add the prefix "UTC:" to your mask.
46 now.format("UTC:h:MM:ss TT Z");
47 // 10:46:21 PM UTC
javascript 日期格式化【转】
最新推荐文章于 2025-12-20 12:23:55 发布
本文介绍了一个强大的日期格式化工具,支持多种格式和国际化的日期时间处理。通过使用该工具,开发者可以轻松地将日期转换为所需的格式,并支持UTC时间转换。
1354

被折叠的 条评论
为什么被折叠?



