Provides various official and user-released code examples and code reference. You are welcome to exchange and learn.
/**
* The same time stamp 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 () {return f. l (). 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 () {return f. F (). 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;
});
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB