JavaScript format date, convert time date format
//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.18Date.prototype.Format =function(FMT) {varo = { "m+": This. GetMonth () + 1,//Month"D+": This. GetDate (),//Day"H +": This. GetHours (),//hours"m+": This. getminutes (),//points"S+": This. getseconds (),//seconds"q+": Math.floor (( This. GetMonth () + 3)/3),//Quarterly"S": This. Getmilliseconds ()//milliseconds }; if(/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); for(varKincho)if(NewRegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); returnFMT;} varHAOROOMS1 =NewDate (). Format ("Yyyy-mm-dd"); varHaorooms2=NewDate (). Format ("Yyyy-mm-dd hh:mm:ss"); document.write (HAOROOMS1); Output 2016-02-18 document.write ("<br>"); document.write (HAOROOMS2);//Output 2016-02-18 10:22:14
Output Result:
2016-02-18
2016-02-18 10:22:14
JavaScript format date, convert time date format