JS formatting the date is really a pain point.
After turning online for half a day, I finally found a very good function. Paste it to take notes.
[Javascript]
/**
* Time object formatting
*/
Date. prototype. format = function (format)
{
/*
* Format = "yyyy-MM-dd hh: mm: ss ";
*/
Var o = {
"M +": this. getMonth () + 1,
"D +": this. getDate (),
"H +": this. getHours (),
"M +": this. getMinutes (),
"S +": this. getSeconds (),
"Q +": Math. floor (this. getMonth () + 3)/3 ),
"S": this. getMilliseconds ()
}
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;
}
/**
* Time object formatting
*/
Date. prototype. format = function (format)
{
/*
* Format = "yyyy-MM-dd hh: mm: ss ";
*/
Var o = {
"M +": this. getMonth () + 1,
"D +": this. getDate (),
"H +": this. getHours (),
"M +": this. getMinutes (),
"S +": this. getSeconds (),
"Q +": Math. floor (this. getMonth () + 3)/3 ),
"S": this. getMilliseconds ()
}
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;
}
Call example:
New Date (). format ("yyyy-MM-dd hh: mm: ss ");
Result: 15:44:26
Thank you!
St. format ("yyyy-MM-dd hh: mm: ss"); st. format ("yyyy-MM-dd hh: mm: ss"); st. format ("yyyy-MM-dd hh: mm: ss ");