One,JavaScript date format (date formatting)
Method One:
//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;}
Call Mode:
new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss"); console.log(time1); //2017-06-26console.log(time2); //2017-06-26 14:52:30
Method Two:
/** * For the extension of date, the date is converted to a specified format of string * months (M), Day (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (q) can be used with 1-2 placeholders * year (y) can use 1-4 placeholders , milliseconds (S) can only be used with 1 placeholders (1-3 digits) * Eg: * (new Date ()). Pattern ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423 * (New Date ()). Pattern (" Yyyy-mm-dd E HH:mm:ss ") ==> 2009-03-10 II 20:09:04 * (new Date ()). Pattern ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 * (new Date ()). Pattern ("Yyyy-mm-dd E EE hh:mm:ss ") ==> 2009-03-10 Tuesday 08:09:04 * (New Date ()). Pattern (" yyyy-m-d h:m:s.s ") ==> 2006-7-2 8:9:4.18 */Date.prototype.pattern=function(FMT) {varo = { "m+": This. GetMonth () +1,//Month"D+": This. GetDate (),//Day"H +": This. getHours ()%12 = = 0? 12: This. GetHours ()%12,//hours"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)); } if(/(e+)/. Test (FMT)) {FMT=fmt.replace (regexp.$1, (regexp.$1.length>1)? (regexp.$1.length>2?) "Week": "Week"): "" + "Day 123456". CharAt ( This. GetDay ())); } 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; }
Call Mode:
var date = new Date(); console.log(date.pattern("yyyy-MM-dd EEE hh:mm:ss")); //2017-06-26 星期一 14:54:12console.log(date.pattern("yyyy-MM-dd EE hh:mm:ss")); //2017-06-26 周一 14:54:12console.log(date.pattern("yyyy-MM-dd E hh:mm:ss")); //2017-06-26 一 14:54:12
Second, timestamp format as date format
Method One:
//Convert timestamps to datetime 2070-1-15 13:56:39functionFormatDate (now) {varYear=now.getyear (); varMonth=now.getmonth (+1); varDate=now.getdate (); varHour=now.gethours (); varMinute=now.getminutes (); varSecond=now.getseconds (); return"+year+"-"+month+"-"+date+" "+hour+": "+minute+": "+second;} varD=NewDate (1230999938); Console.log (FormatDate (d)); //2070-1-15 13:56:39
Method Two:
function returnnew Date (parseint (NS) *). toLocaleString (). Replace (/:\d{ 1,2}$/, "); } console.log (Getlocaltime (1293072805)); //
JS Date/Time Format method