/*
* date format 1.2.3
* (c) 2007-2009 steven levithan
* mit license
*
* includes enhancements by scott trenda
* and kris kowal
*
* accepts a date, a mask, or a date and a mask.
* returns a formatted version of the given date.
* the date defaults to the current date/time.
* the mask defaults to dateformat.masks.default.
*/
var dateformat = function () {
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([hhmstt])\1?|[llosz]|"[^"]*"|'[^']*'/g,
timezone = /\b(?:[pmcea][sdp]t|(?:pacific|mountain|central|eastern|atlantic) (?:standard|daylight|prevailing) time|(?:gmt|utc)(?:[-+]\d{4})?)\b/g,
timezoneclip = /[^-+\da-z]/g,
pad = function (val, len) {
val = string(val);
len = len || 2;
while (val.length < len) val = "0" + val; return val; }; // regexes and supporting functions are cached through closure return function (date, mask, utc) { var df = dateformat; // you can't provide utc if you skip other args (use the "utc:" mask prefix) if (arguments.length == 1 && object.prototype.tostring.call(date) == "[object string]" && !/\d/.test(date)) { mask = date; date = undefined; } // passing date through date applies date.parse, if necessary date = date ? new date(date) : new date; if (isnan(date)) throw syntaxerror("invalid date"); mask = string(df.masks[mask] || mask || df.masks["default"]); // allow setting the utc argument via the mask if (mask.slice(0, 4) == "utc:") { mask = mask.slice(4); utc = true; } var _ = utc ? "getutc" : "get", d = date[_ + "date"](), d = date[_ + "day"](), m = date[_ + "month"](), y = date[_ + "fullyear"](), h = date[_ + "hours"](), m = date[_ + "minutes"](), s = date[_ + "seconds"](), l = date[_ + "milliseconds"](), o = utc ? 0 : date.gettimezoneoffset(), flags = { d: d, dd: pad(d), ddd: df.i18n.daynames[d], dddd: df.i18n.daynames[d + 7], m: m + 1, mm: pad(m + 1), mmm: df.i18n.monthnames[m], mmmm: df.i18n.monthnames[m + 12], yy: string(y).slice(2), yyyy: y, h: h % 12 || 12, hh: pad(h % 12 || 12), h: h, hh: pad(h), m: m, mm: pad(m), s: s, ss: pad(s), l: pad(l, 3), l: pad(l > 99 ? math.round(l / 10) : l),
t: h < 12 ? "a" : "p",
tt: h < 12 ? "am" : "pm",
t: h < 12 ? "a" : "p",
tt: h < 12 ? "am" : "pm", z: utc ? "utc" : (string(date).match(timezone) || [""]).pop().replace(timezoneclip, ""), o: (o > 0 ? "-" : "+") + pad(math.floor(math.abs(o) / 60) * 100 + math.abs(o) % 60, 4),
s: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};
return mask.replace(token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
}();
// some common format strings
dateformat.masks = {
"default": "ddd mmm dd yyyy hh:mm:ss",
shortdate: "m/d/yy",
mediumdate: "mmm d, yyyy",
longdate: "mmmm d, yyyy",
fulldate: "dddd, mmmm d, yyyy",
shorttime: "h:mm tt",
mediumtime: "h:mm:ss tt",
longtime: "h:mm:ss tt z",
isodate: "yyyy-mm-dd",
isotime: "hh:mm:ss",
isodatetime: "yyyy-mm-dd't'hh:mm:ss",
isoutcdatetime: "utc:yyyy-mm-dd't'hh:mm:ss'z'"
};
// internationalization strings
dateformat.i18n = {
daynames: [
"sun", "mon", "tue", "wed", "thu", "fri", "sat",
"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
],
monthnames: [
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"
]
};
// for convenience...
date.prototype.format = function (mask, utc) {
return dateformat(this, mask, utc);
};
用法:
var now = new date();
now.format("m/dd/yy");
// returns, e.g., 6/09/07
// can also be used as a standalone function
dateformat(now, "dddd, mmmm ds, yyyy, h:mm:ss tt");
// saturday, june 9th, 2007, 5:46:21 pm
// you can use one of several named masks
now.format("isodatetime");
// 2007-06-09t17:46:21
// ...or add your own
dateformat.masks.hammertime = 'hh:mm! "can\'t touch this!"';
now.format("hammertime");
// 17:46! can't touch this!
// when using the standalone dateformat function,
// you can also provide the date as a string
dateformat("jun 9 2007", "fulldate");
// saturday, june 9, 2007
// note that if you don't include the mask argument,
// dateformat.masks.default is used
now.format();
// sat jun 09 2007 17:46:21
// and if you don't include the date argument,
// the current date and time is used
dateformat();
// sat jun 09 2007 17:46:22
// you can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
dateformat("longtime");
// 5:46:22 pm est
// and finally, you can convert local time to utc time. either pass in
// true as an additional argument (no argument skipping allowed in this case):
dateformat(now, "longtime", true);
now.format("longtime", true);
// both lines return, e.g., 10:46:21 pm utc
// ...or add the prefix "utc:" to your mask.
now.format("utc:h:mm:ss tt z");
// 10:46:21 pm utc
mask |
description |
d |
day of the month as digits; no leading zero for single-digit days. |
dd |
day of the month as digits; leading zero for single-digit days. |
ddd |
day of the week as a three-letter abbreviation. |
dddd |
day of the week as its full name. |
m |
month as digits; no leading zero for single-digit months. |
mm |
month as digits; leading zero for single-digit months. |
mmm |
month as a three-letter abbreviation. |
mmmm |
month as its full name. |
yy |
year as last two digits; leading zero for years less than 10. |
yyyy |
year represented by four digits. |
h |
hours; no leading zero for single-digit hours (12-hour clock). |
hh |
hours; leading zero for single-digit hours (12-hour clock). |
h |
hours; no leading zero for single-digit hours (24-hour clock). |
hh |
hours; leading zero for single-digit hours (24-hour clock). |
m |
minutes; no leading zero for single-digit minutes. uppercase m unlike cf timeformat 's m to avoid conflict with months. |
mm |
minutes; leading zero for single-digit minutes. uppercase mm unlike cf timeformat 's mm to avoid conflict with months. |
s |
seconds; no leading zero for single-digit seconds. |
ss |
seconds; leading zero for single-digit seconds. |
l or l |
milliseconds. l gives 3 digits. l gives 2 digits. |
t |
lowercase, single-character time marker string: a or p. no equivalent in cf. |
tt |
lowercase, two-character time marker string: am or pm. no equivalent in cf. |
t |
uppercase, single-character time marker string: a or p. uppercase t unlike cf's t to allow for user-specified casing. |
tt |
uppercase, two-character time marker string: am or pm. uppercase tt unlike cf's tt to allow for user-specified casing. |
z |
us timezone abbreviation, e.g. est or mdt. with non-us timezones or in the opera browser, the gmt/utc offset is returned, e.g. gmt-0500 no equivalent in cf. |
o |
gmt/utc timezone offset, e.g. -0500 or +0230. no equivalent in cf. |
s |
the date's ordinal suffix (st, nd, rd, or th). works well with d . no equivalent in cf. |
'…' or "…" |
literal character sequence. surrounding quotes are removed. no equivalent in cf. |
utc: |
must be the first four characters of the mask. converts the date from local time to utc/gmt/zulu time before applying the mask. the “utc:” prefix is removed. no equivalent in cf. |
name |
mask |
example |
default |
ddd mmm dd yyyy hh:mm:ss |
sat jun 09 2007 17:46:21 |
shortdate |
m/d/yy |
6/9/07 |
mediumdate |
mmm d, yyyy |
jun 9, 2007 |
longdate |
mmmm d, yyyy |
june 9, 2007 |
fulldate |
dddd, mmmm d, yyyy |
saturday, june 9, 2007 |
shorttime |
h:mm tt |
5:46 pm |
mediumtime |
h:mm:ss tt |
5:46:21 pm |
longtime |
h:mm:ss tt z |
5:46:21 pm est |
isodate |
yyyy-mm-dd |
2007-06-09 |
isotime |
hh:mm:ss |
17:46:21 |
isodatetime |
yyyy-mm-dd't'hh:mm:ss |
2007-06-09t17:46:21 |
isoutcdatetime |
utc:yyyy-mm-dd't'hh:mm:ss'z' |
2007-06-09t22:46:21z |
如对本文有疑问,
点击进行留言回复!!
网友评论