An extension to date that converts date to a string of the specified format
The month (m), Day (d), hour (h), Minute (m), second (s), quarter (q) can be used with 1-2 placeholders,
Year (y) can use 1-4 placeholders, milliseconds (S) with only 1 placeholders (1-3 digits)
Example:
(New Date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423
(New Date ()). Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (FMT) {//author:meizz
var o = {
"m+": This.getmonth () + 1,//month
"D+": this.getdate (),//day
"H +": this.gethours (),//hour
"m+": this.getminutes (),//min
"S+": This.getseconds (),//sec
"q+": Math.floor ((This.getmonth () + 3)/3),//Quarterly
"S": this.getmilliseconds ()//MS
};
if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O)
if (New RegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).));
return FMT;
}
Call:
var time1 = new Date (). Format ("Yyyy-mm-dd"); var time2 = new Date (). Format ("Yyyy-mm-dd HH:mm:ss");
JavaScript date format (JS dates formatted)