Although a Date object can be obtained using the new date (), it must be converted to the format that the developer wants.
This provides an encapsulation method that, by combining the use of regular expressions, achieves the purpose of processing time objects and generating multiple date formats.
function DateFormat (date,format) {
var o = {
"m+": Date.getmonth () +1,//month
"d+": Date.getdate (),//day
"H +": date.gethours (),//hour
"m+": Date.getminutes (),//minute
"s+": Date.getseconds (),//second
"q+": Math.floor ((Date.getmonth () +3)/3),//quarter
"S": Date.getmilliseconds ()//millisecond
}
if (/(y+)/.test (format)) Format=format.replace (regexp.$1,
(Date.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]:
("xx" + o[k]). substr (("" + o[k] ). length);
return format;
}//Call
Console.log (DateFormat (New Date (), ' yyyy-mm-dd '));
Console.log (DateFormat (New Date (), ' mm-yyyy-dd '));
Common encapsulation--date--Get formatted Date object