CopyCode The Code is 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? | [Losz] | "[^"] * "| '[^'] * '/g,
Timezone =/\ B (? : [Pmcea] [SDP] T | (? : Pacific | mountain | central | Eastern | Atlanta )(? : 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 ",
Expiry date: "m/D/YY ",
Mediumdate: "Mmm D, YYYY ",
Longdate: "Mmmm D, YYYY ",
Fulldate: "dddd, Mmmm D, YYYY ",
Processing time: "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", "maid", "may", "June", "July", "August", "September", "October ", "November", "December"
]
};
// For convenience...
Date. Prototype. format = function (mask, UTC ){
Return dateformat (this, mask, UTC );
};
Usage:Copy codeThe Code is 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
// 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 ");
! 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 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 UTC
//... Or add the prefix "UTC:" to your mask.
Now. Format ("UTC: H: mm: ss tt z ");
// 10:46:21 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 cfTimeformat 'S m to avoid conflict with months. |
Mm |
Minutes; leading zero for single-digit minutes. Uppercase mm unlike cfTimeformat '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:AOrP. No equivalent in CF. |
TT |
Lowercase, two-character time marker string:AMOrPM. No equivalent in CF. |
T |
Uppercase, single-character time marker string:AOrP. Uppercase t unlike CF's t to allow for user-specified casing. |
TT |
Uppercase, two-character time marker string:AMOrPM. Uppercase TT unlike CF's TT to allow for user-specified casing. |
Z |
US timezone abbreviation, e.g.ESTOrMDT. 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.-0500Or+ 0230. No equivalent in CF. |
S |
The date's ordinal suffix (St,Nd,Rd, OrTh). Works wellD . 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 |
Expiry date |
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 |
Processing time |
H: Mm TT |
5: 46 pm |
Mediumtime |
H: mm: SS TT |
5:46:21 |
Longtime |
H: mm: SS TT Z |
5:46:21 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 |