Copy codeThe Code is as follows:
// The same timestamp Formatting Function as PHP
// @ Param {string} format
// @ Param {int} timestamp the time to be formatted is the current time by default
// @ Return {string} the formatted time string
Function date (format, timestamp ){
Var a, jsdate = (timestamp )? New Date (timestamp * 1000): new Date ());
Var pad = function (n, c ){
If (n = n + ""). length <c ){
Return new Array (++ c-n. length). join ("0") + n;
} Else {
Return n;
}
};
Var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Var txt_ordin = {1: "st", 2: "nd", 3: "rd", 21: "st", 22: "nd", 23: "rd ", 31: "st "};
Var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August ", "September", "October", "November", "December"];
Var f = {
// Day
D: function (){
Return pad (f. j (), 2 );
},
D: function (){
T = f. l (); return t. substr (0, 3 );
},
J: function (){
Return jsdate. getDate ();
},
L: function (){
Return txt_weekdays [f. w ()];
},
N: function (){
Return f. w () + 1;
},
S: function (){
Return txt_ordin [f. j ()]? Txt_ordin [f. j ()]: 'th ';
},
W: function (){
Return jsdate. getDay ();
},
Z: function (){
Return (jsdate-new Date (jsdate. getFullYear () + "/1/1")/864e5> 0;
},
// Week
W: function (){
Var a = f. z (), B = 364 + f. L ()-;
Var nd2, nd = (new Date (jsdate. getFullYear () + "/1/1"). getDay () | 7)-1;
If (B <= 2 & (jsdate. getDay () | 7)-1) <= 2-B ){
Return 1;
} Else {
If (a <= 2 & nd> = 4 & a> = (6-nd )){
Nd2 = new Date (FIG ()-1 + "/12/31 ");
Return date ("W", Math. round (nd2.getTime ()/1000 ));
} Else {
Return (1 + (nd <= 3? (A + nd)/7): (a-(7-nd)/7)> 0 );
}
}
},
// Month
F: function (){
Return txt_months [f. n ()];
},
M: function (){
Return pad (f. n (), 2 );
},
M: function (){
T = f. F (); return t. substr (0, 3 );
},
N: function (){
Return jsdate. getMonth () + 1;
},
T: function (){
Var n;
If (n = jsdate. getMonth () + 1) = 2 ){
Return 28 + f. L ();
} Else {
If (n & 1 & n <8 |! (N & 1) & n> 7 ){
Return 31;
} Else {
Return 30;
}
}
},
// Year
L: function (){
Var y = f. Y ();
Return (! (Y & 3) & (y % 1e2 |! (Y % 4e2 )))? 1: 0;
},
// O not supported yet
Y: function (){
Return jsdate. getFullYear ();
},
Y: function (){
Return (jsdate. getFullYear () + ""). slice (2 );
},
// Time
A: function (){
Return jsdate. getHours ()> 11? "Pm": "am ";
},
A: function (){
Return f. a (). toUpperCase ();
},
B: function (){
// Peter paul koch:
Var off = (jsdate. getTimezoneOffset () + 60) * 60;
Var theSeconds = (jsdate. getHours () * 3600) +
(Jsdate. getMinutes () * 60) +
Jsdate. getSeconds () + off;
Var beat = Math. floor (theSeconds/86.4 );
If (beat> 1000) beat-= 1000;
If (beat <0) beat + = 1000;
If (String (beat). length = 1) beat = "00" + beat;
If (String (beat). length = 2) beat = "0" + beat;
Return beat;
},
G: function (){
Return jsdate. getHours () % 12 | 12;
},
G: function (){
Return jsdate. getHours ();
},
H: function (){
Return pad (f. g (), 2 );
},
H: function (){
Return pad (jsdate. getHours (), 2 );
},
I: function (){
Return pad (jsdate. getMinutes (), 2 );
},
S: function (){
Return pad (jsdate. getSeconds (), 2 );
},
// U not supported yet
// Timezone
// E not supported yet
// I not supported yet
O: function (){
Var t = pad (Math. abs (jsdate. getTimezoneOffset ()/60*100), 4 );
If (jsdate. getTimezoneOffset ()> 0) t = "-" + t; else t = "+" + t;
Return t;
},
P: function (){
Var O = f. O ();
Return (O. substr (0, 3) + ":" + O. substr (3, 2 ));
},
// T not supported yet
// Z not supported yet
// Full Date/Time
C: function (){
Return f. Y () + "-" + f. m () + "-" + f. d () + "T" + f. h () + ":" + f. I () + ":" + f. s () + f. P ();
},
// R not supported yet
U: function (){
Return Math. round (jsdate. getTime ()/1000 );
}
};
Return format. replace (/[\]? ([A-zA-Z])/g, function (t, s ){
If (t! = S ){
// Escaped
Ret = s;
} Else if (f [s]) {
// A date function exists
Ret = f [s] ();
} Else {
// Nothing special
Ret = s;
}
Return ret;
});
}