Javascript Date format (js Date format), datejs
Method 1:
// Extend the Date to convert the Date to a String in the specified format
// One to two placeholders can be used for month (M), Day (d), hour (h), minute (m), second (s), and quarter (q,
// 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") => 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 (), // hour
"M +": this. getMinutes (), // minute
"S +": this. getSeconds (), // second
"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">
<! --/*** Extend Date to 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, // hour
"H +": this. getHours (), // hour
"M +": this. getMinutes (), // minute
"S +": this. getSeconds (), // second
"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;
Var zeroize = function (value, length ){
If (! Length) length = 2;
Value = String (value );
For (var I = 0, zeros = ''; I <(length-value. length); I ++ ){
Zeros + = '0 ';
}
Return zeros + value;
};
Return mask. replace (/"[^"] * "| '[^'] * '|/B (? : D {
1, 4
} | M {
1, 4
} | Yy (? : Yy )? | ([HHMstT])/1? | [LLZ])/B/g, function ($0 ){
Switch ($0 ){
Case 'D ':
Return d. getDate ();
Case 'dd ':
Return zeroize (d. getDate ());
Case 'ddd ':
Return ['sun', 'mon ', 'tue', 'wed', 'thr', 'fri', 'sat '] [d. getDay ()];
Case 'ddddd ':
Return ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'Friday', 'saturday'] [d. getDay ()];
Case 'M ':
Return d. getMonth () + 1;
Case 'mm ':
Return zeroize (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 ':
Return String (d. getFullYear (). substr (2 );
Case 'yyyy ':
Return d. getFullYear ();
Case 'H ':
Return d. getHours () % 12 | 12;
Case 'hh ':
Return zeroize (d. getHours () % 12 | 12 );
Case 'H ':
Return d. getHours ();
Case 'hh ':
Return zeroize (d. getHours ());
Case 'M ':
Return d. getMinutes ();
Case 'mm ':
Return zeroize (d. getMinutes ());
Case's ':
Return d. getSeconds ();
Case 'ss ':
Return zeroize (d. getSeconds ());
Case 'l ':
Return zeroize (d. getMilliseconds (), 3 );
Case 'l ':
Var m = d. getMilliseconds ();
If (m> 99) m = Math. round (m/10 );
Return zeroize (m );
Case 'TT ':
Return d. getHours () <12? 'Am': 'pm ';
Case 'TT ':
Return d. getHours () <12? 'Am': 'pm ';
Case 'Z ':
Return d. toUTCString (). match (/[A-Z] + $ /);
// Return quoted strings with the surrounding quotes removed
Default:
Return $0. substr (1, $0. length-2 );
}
});
};
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.