Date Object Format Function code _ time Date

Source: Internet
Author: User
Tags abs dateformat i18n local time lowercase
Copy Code code as follows:

/*
* 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| (?:P acific| 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 return function (date, closure, UTC) {var DF = mask;// Can ' t provide UTC if you skip other args (Use the "UTC:" Mask prefix) if (arguments.length = 1 && object.pro Totype.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: 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 +), yy:string (y). Slice (2), yyyy:y, h:h% 1 2 | | Hh:pad (h%), 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) | | [""]). POPs (). replace (Timezoneclip, ""), O: (o > 0?) -":" + ") + pad (Math.floor (Math.Abs (o)/p) * + math.abs (o)% 60, 4),
S: ["th", "st", "nd", "rd"][d% > 3? 0: (d% 100-d%!=) * d% 10]
};

Return Mask.replace (token, function ($) {
Return $ 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: [
"The Feb", "the", "The Mar", "APR", "may", "June", "may", "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);
};

Usage:
Copy Code code as follows:

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 ("June 9 2007", "Fulldate");
Saturday, June 9, 2007

Note this if you don ' t include the mask argument,
DateFormat.masks.default is used
Now.format ();
Sat June 09 2007 17:46:21

And if you don ' t include the date argument,
The current date and used
DateFormat ();
Sat June 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 the local time to UTC time. Either pass in
True as a 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 the single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as it 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.
lorL milliseconds. lgives 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 of the 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, with d .
No equivalent in CF.
'…'or"…" Literal character sequence. Surrounding quotes are removed.
No equivalent in CF.
UTC: Must be the four characters of the mask. Converts the "date from" 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 June 09 2007 17:46:21
ShortDate M/d/yy 6/9/07
Mediumdate Mmm d, yyyy June 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.