1 /**2 * @author Today3 * If you want to format the date string, you need to convert it to date type4 * Here are a few of the most common5 *6 * var da = new Date (). Format (' Yyyy-mm-dd hh:mm:ss ');//Convert the date format string to the first format7 * Alert ("format date type \ n" + new Date () + "\ n = string:" + da);8 *9 * var str = "2014/01/01 01:01:01"//YYYY/MM/DD this format is converted into date pairs like can be used with new date (str);Ten * Alert ("formatted string \ n" + str + "for date format \ n" + new Date (str). Format (' Yyyy-mm-dd hh:mm:ss ')) One * A * - * var str1 = "2014-12-31 00:55:55"//YYYY-MM-DD this format string into date object can be used with the new date (Date.parse (Str.replace, "/"))); - * Alert ("formatted string \ n" + str1 + "for date format \ n" + new Date (Date.parse (Str1.replace (/-/g, "/")). Format (' Yyyy-mm-dd hh:mm:ss ')) the * - * - * Date plus month - * Converts a character to a date type before it can be used + * var str1 = "2014-12-31 00:55:55"//YYYY-MM-DD this format string into date object can be used with the new date (Date.parse (Str.replace, "/"))); - * For example var savedate = new Date (Date.parse (Str1.replace (/-/g, "/")). Addmonth (5) + * Addmonth (number of months) must be an integer A */ at -Date.prototype.format =function(format) { - varDate = { -"m+": This. GetMonth () + 1, -"D+": This. GetDate (), -"H +": This. GetHours (), in"m+": This. getminutes (), -"S+": This. getseconds (), to"q+": Math.floor (( This. GetMonth () + 3)/3), +"S+": This. Getmilliseconds () - }; the if(/(y+)/i.test (format)) { *Format = Format.replace (Regexp.$1, ( This. getFullYear () + "). substr (4-regexp.$1. length)); $ }Panax Notoginseng for(varKinchdate) { - if(NewRegExp ("(" + K + ")"). Test (format)) { theFormat = Format.replace (regexp.$1, regexp.$1.length = = 1 +? Date[k]: ("XX" + date[k]). substr ("" +Date[k]) (length)); A } the } + returnformat; - } $Date.daysinmonth =function(year, month) { $ if(Month = = 1) { - if(year% 4 = = 0 && Year% 100! = 0) - return29; the Else - return28;Wuyi}Else if((Month <= 6 && Month% 2 = = 0) | | (month = 6 && Month% 2 = = 1)) the return31; - Else Wu return30; - }; AboutDate.prototype.addMonth =function(addmonth) { $ vary = This. getFullYear (); - varm = This. GetMonth (); - varNexty =y; - varNEXTM =m; A //if the current month + to add the month >11 here is used 11 because JS month starting from 0 + if(M > 11) { theNexty = y + 1; -Nextm = parseint (M + addmonth)-11; $}Else { theNEXTM = This. GetMonth () +Addmonth the } the varDaysinnextmonth =Date.daysinmonth (nexty, NEXTM); the varDay = This. GetDate (); - if(Day >daysinnextmonth) { inDay =Daysinnextmonth; the } the return NewDate (Nexty, NEXTM, day); About};
JS string to date format date format string