1 //time-of-day prototypes add formatting methods2 3Date.prototype.Format =function(formatstr) {4 varstr =Formatstr;5 varWeek = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];6 7str = str.replace (/yyyy| yyyy/, This. getFullYear ());8str = str.replace (/yy| yy/, ( This. GetYear ()%) > 9? ( This. GetYear ()%). ToString (): ' 0 ' + ( This. GetYear ()% 100));9 varmonth = This. GetMonth () + 1;Tenstr = str.replace (/mm/, month > 9? month.tostring (): ' 0 ' +month); Onestr = str.replace (/m/G, month); A -str = str.replace (/w| W/g, week[ This. GetDay ()]); - thestr = 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 ()); A atstr = str.replace (/ss| ss/, This. getseconds () > 9? This. getseconds (). toString (): ' 0 ' + This. getseconds ()); -str = str.replace (/s| S/g, This. getseconds ()); - returnstr; - }
It's easier to call,
1 var d=New Date (); 2 var str=d.format ("Yyyy-mm-dd hh:mm:ss"); 3 console.log (str);
A date processing format function in JS