Java code
// Convert the date type to string
<! --
/**
* Extended Date: converts Date to a String in the specified format.
* Month (M), Day (d), 12 hours (h), 24 hours (H), minute (m), second (s), Week (E), quarter (q) can use 1-2 placeholders
* Year (y) can use 1-4 placeholders, Millisecond (S) can only use 1 placeholder (1-3 digits)
* Eg:
* (New Date (). pattern ("yyyy-MM-dd hh: mm: ss. S") => 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
*/
// Var date = new Date ();
// Window. alert (date. pattern ("yyyy-MM-dd hh: mm: ss "));
// -->
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;
}
Author "sljackson"