Format js Date functions

Source: Internet
Author: User
Tags abs dateformat i18n local time touch


/*
* Date format 1.2.3
* (III) 2007-2009 Steven levithan
* Mit license
*
* Includes enhancements by scott trentda
* And Christopher
*
* Accept a date, a mask, or a date and a mask.
* Returns a version of the given date format.
* The default date is the current date/time.
* The default mask is dateformat. masks. default. */

Var dateformat = function (){
Var token =/d {1, 4} | m {1, 4} | yy (? : Yy )? | ([Hhmstt])? | [Losz] | "[^"] * "| '[^'] * '/g,
Timezone = /(? : [Pmcea] [sdp] t | (? : Pacific | mountain | central | eastern | Atlanta )(? : Standard | daylight | prevailing) time | (? : Gmt | utc )(? : [-+] D {4 })?) /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

 

Var now = new date ();

Now. format ("m/dd/yy ");

// Return, for example, 6/09/07

// It can also be used as an independent function
Dateformat (now, "dddd will, um, deputy director of the bureau, West yuan Year, High: mm: ss ");
// Saturday, August 1, June 9, 2007, 05:46:21 P.M.

// You can use multiple naming masks 1
Now. format ("isodatetime ");
// 2007-06-09t17: 46: 21

//... Or add your own
Dateformat. masks. hammertime = 'is hh: mm! "You can't touch this !";
Now. format ("hammertime ");
! Cannot touch this!

// When The Independent dateformat function is used,
// You can also provide a string date
Dateformat ("may June 9, 2007", "fulldate ");
// Saturday, January 1, June 9, 2007

// Note that if you do not include the arguments for masks,
// Dateformat. masks. default
Now. format ();
// Saturday June 9, 2007 17:46:21

// If you do not include the date parameter,
// The current date and time are used
Dateformat ();
// Saturday, December 17, June 9, 2007, 22 seconds

// You can also skip the date parameter (as long as you do not mask
// Contains any number). In this case, the current date/time is used
Dateformat ("long term ");
// 05:46:22 P.M. Eastern time

// Finally, you can convert the local time to utc time. Either through
// Actually acts as an additional parameter (in this case there is no reason to skip ):
Dateformat (now "old", true );
Now. format ("long term", true );
// Return of the two lines, for example, 10:46:21 P.M. Sunday

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.