Javascript Date Format (JS Date Format)

Source: Internet
Author: User

Method 1:

// Converts a date to a string in the specified format.
// Month (M), Day (D), hour (H), minute (M), second (s), quarter (q) can use 1-2 placeholders,
// Year (y) can use 1-4 placeholders, Millisecond (s) can only use 1 placeholder (1-3 digits)
// Example:
// (New date (). Format ("yyyy-mm-dd hh: mm: Ss. s") ==>2006-07-02 08:09:04. 423
// (New date (). Format ("yyyy-m-d h: M: S. S") ==> 2006-7-2. 18
Date. Prototype. format = Function (FMT ){ // Author: meizz
VaR O = {
"M + ": This . Getmonth () + 1, // Month
"D + ": This . Getdate (), // Day
"H + ": This . Gethours (), // Hours
"M + ": This . Getminutes (), // Minute
"S + ": This . Getseconds (), // Seconds
"Q +": Math. Floor (( This . Getmonth () + 3)/3 ), // Quarter
"S ": This . Getmilliseconds () // Millisecond
};
If (/(Y +)/. Test (FMT) FMt = FMT. Replace (Regexp. $1 ,( This . Getfullyear () + ""). substr (4-Regexp. $1. Length ));
For ( VaR K In O)
If ( New Regexp ("(" + K + ")"). test (FMT) FMt = FMT. replace (Regexp. $1, (Regexp. $1. length = 1 )? (O [k]): ("00" + O [k]). substr ("" + O [k]). Length )));
Return FMT;
}

Call:

VaR time1 = new date (). format ("yyyy-mm-dd"); var time2 = new date (). format ("yyyy-mm-dd hh: mm: SS ");

Method 2:

<Script language = "JavaScript" type = "text/JavaScript">
<! -- /* ** Extended Date: converts a date to a string * month (M), Day (D), 12 hours (H), and 24 hours (h) in the specified format), minute (M), second (s), Week (E), quarter (q)
You can use 1-2 placeholders * Year (Y), 1-4 placeholders, Millisecond (s), and only 1 placeholder (1-3 digits) * eg: * (New
Date (). pattern ("yyyy-mm-dd hh: mm: Ss. s") ==>2006-07-02 08:09:04. 423
* (New date (). pattern ("yyyy-mm-dd e hh: mm: SS") => 20:09:04
* (New date (). pattern ("yyyy-mm-dd EE hh: mm: SS") => 08:09:04 Tuesday
* (New date (). pattern ("yyyy-mm-dd EEE hh: mm: SS") => 08:09:04 Tuesday
* (New date (). pattern ("yyyy-m-d h: M: S. S") => 2006-7-2. 18
*/
Date. Prototype. pattern = Function (FMT ){
VaR O = {
"M + ": This . Getmonth () + 1, // Month
"D + ": This . Getdate (), // Day
"H + ": This . Gethours () % 12 = 0? 12: This . Gethours () % 12, // Hours
"H + ": This . Gethours (), // Hours
"M + ": This . Getminutes (), // Minute
"S + ": This . Getseconds (), // Seconds
"Q +": Math. Floor (( This . Getmonth () + 3)/3), // quarter
"S ": This . Getmilliseconds () // Millisecond
};
VaR Week = {
"0": "/u65e5 ",
"1": "/u4e00 ",
"2": "/u4e8c ",
"3": "/u4e09 ",
"4": "/u56db ",
"5": "/u4e94 ",
"6": "/u516d"
};
If (/(Y +)/. Test (FMT )){
FMt = FMT. Replace (Regexp. $1 ,( This . Getfullyear () + ""). substr (4-Regexp. $1. Length ));
}
If (/(E +)/. Test (FMT )){
FMt = FMT. Replace (Regexp. $1, (Regexp. $1. length> 1 )? (Regexp. $1. length> 2? "/U661f/u671f": "/u5468"): "") + week [ This . Getday () + ""]);
}
For ( VaR K In O ){
If ( New Regexp ("(" + K + ")"). Test (FMT )){
FMt = FMT. Replace (Regexp. $1, (Regexp. $1. Length = 1 )? (O [k]): ("00" + O [k]). substr ("" + O [k]). Length )));
}
}
Return FMT;
}

VaR Date = New Date ();
Window. Alert (date. pattern ("yyyy-mm-dd hh: mm: SS "));
// -->
</SCRIPT>

Method 3:

Date. Prototype. format = Function (Mask ){
VaR D = This ;

VaRZeroize =Function(Value, length ){

If(! Length) Length = 2;

Value = string (value );

For(VaRI = 0, zeros = ''; I <(length-value. Length); I ++ ){

Zeros + = '0 ';

}

ReturnZeros + value;

};

ReturnMask. Replace (/"[^"] * "| '[^'] * '|/B (? : D {
1, 4
} | M {
1, 4
} | YY (? : Yy )? | ([Hhmstt])/1? | [Llz])/B/g,Function($0 ){

Switch($0 ){

Case'D ':
ReturnD. getdate ();

Case'Dd ':
ReturnZeroize (D. getdate ());

Case'Ddd ':
Return['Sun', 'mon ', 'tue', 'wed', 'thr', 'fri', 'sat '] [D. getday ()];

Case'Dddddd ':
Return['Sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'Friday', 'saturday'] [D. getday ()];

Case'M ':
ReturnD. getmonth () + 1;

Case'Mm ':
ReturnZeroize (D. getmonth () + 1 );

Case'Mmm ':
Return['Jan ', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep ', 'oct', 'nov', 'dec '] [D. getmonth ()];

Case'Mmmm ':
Return['January ', 'february', 'march', '0000l', 'may', 'june', 'july', 'August ', 'September ', 'October ', 'november', 'december'] [D. getmonth ()];

Case'Yy ':
ReturnString (D. getfullyear (). substr (2 );

Case'Yyyy ':
ReturnD. getfullyear ();

Case'H ':
ReturnD. gethours () % 12 | 12;

Case'Hh ':
ReturnZeroize (D. gethours () % 12 | 12 );

Case'H ':
ReturnD. gethours ();

Case'Hh ':
ReturnZeroize (D. gethours ());

Case'M ':
ReturnD. getminutes ();

Case'Mm ':
ReturnZeroize (D. getminutes ());

Case'S ':
ReturnD. getseconds ();

Case'Ss ':
ReturnZeroize (D. getseconds ());

Case'L ':
ReturnZeroize (D. getmilliseconds (), 3 );

Case'L ':
VaRM = D. getmilliseconds ();

If(M> 99) M = math. Round (M/10 );

ReturnZeroize (m );

Case'TT ':
ReturnD. gethours () <12? 'Am': 'pm ';

Case'TT ':
ReturnD. gethours () <12? 'Am': 'pm ';

Case'Z ':
ReturnD. toutcstring (). Match (/[A-Z] + $ /);

//Return quoted strings with the surrounding quotes removed

Default:
Return$0. substr (1, $0. Length-2 );

}

});

};

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.