Copy codeThe Code is as follows:
Date. prototype. format = function (format)
{
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;
}
Var testDate = new Date (1320336000000); // The value must be an integer in milliseconds.
Var testStr = testDate. format ("MM-dd-yyyy hh mm min ss seconds ");
Var testStr2 = testDate. format ("yyyyMMdd hh: mm: ss ");
Alert (testStr + "" + testStr2 );