This article mainly introduces examples of js formatting time and js formatting timestamp. For more information, see
The Code is as follows:
/**
* Format the time object;
*/
Date. prototype. format = function (format ){
/*
* Eg: format = "YYYY-MM-dd hh: mm: ss ";
*/
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 (format )){
Format = format. replace (RegExp. $1, (this. getFullYear () + "")
. Substr (4-RegExp. $1. length ));
}
For (var k in o ){
If (new RegExp ("(" + k + ")"). test (format )){
Format = format. replace (RegExp. $1, RegExp. $1. length = 1? O [k]
: ("00" + o [k]). substr ("" + o [k]). length ));
}
}
Return format;
}
The Code is as follows:
Var now = new Date (). format ("yyyy-MM-dd hh: mm: ss ");
The Code is as follows:
New Date (). format ("yy-MM-dd hh: mm ");