Since you often need to format dates in your work, unlike the backend, where convenient methods can be invoked, you can define a format method in the Date object prototype, as shown in the following:
Copy Code code as follows:
Date-time prototypes add formatting methods
Date.prototype.Format = function (formatstr) {
var str = FORMATSTR;
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
str = str.replace (/yyyy| yyyy/, This.getfullyear ());
str = str.replace (/yy| yy/, (this.getyear ()%) > 9? (This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100));
var month = This.getmonth () + 1;
str = str.replace (/mm/, month > 9? month.tostring (): ' 0 ' + month);
str = str.replace (/m/g, month);
str = str.replace (/w| W/g, Week[this.getday ()]);
str = str.replace (/dd| Dd/, This.getdate () > 9? This.getdate (). toString (): ' 0 ' + this.getdate ());
str = str.replace (/d| D/g, This.getdate ());
str = str.replace (/hh| hh/, This.gethours () > 9? This.gethours (). toString (): ' 0 ' + this.gethours ());
str = str.replace (/h| H/g, This.gethours ());
str = str.replace (/mm/, This.getminutes () > 9 this.getminutes (). toString (): ' 0 ' + this.getminutes ());
str = str.replace (/m/g, This.getminutes ());
str = str.replace (/ss| ss/, This.getseconds () > 9? This.getseconds (). toString (): ' 0 ' + this.getseconds ());
str = str.replace (/s| S/g, This.getseconds ());
return str;
}
It is simpler to call, for example:
Copy Code code as follows:
var d = new Date ();
var str = D.format ("Yyyy-mm-dd hh:mm:ss");
Console.log (str);