JS string to date format date format string

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.