This article reproduces address http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html
//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) {//Author:meizz 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:varTime1 =NewDate (). Format ("YYYY-MM-DD");varTime2 =NewDate (). Format ("YYYY-MM-DD HH:mm:ss");
2
<script language="JavaScript"Type="Text/javascript"> <!--/** * 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 ()% A==0? A: This. GetHours ()% A,//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 }; varWeek = { "0":"/u65e5", "1":"/u4e00", "2":"/u4e8c", "3":"/u4e09", "4":"/u56db", "5":"/u4e94", "6":"/u516d" }; 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?"/u661f/u671f":"/u5468") :"") +week[ 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; } varDate =NewDate (); Window.alert (Date.pattern ("YYYY-MM-DD Hh:mm:ss"));// -</script>
JavaScript date format (JS dates formatted) reprint