Regular Expression format date

Source: Internet
Author: User

Generally, the format of a date object obtained from the server (such as C # Code) may be 2014-04-04T13: 04: 46.187. When displaying the date object to the user, you may only need to display the date part, for example: many articles on JavaScript Date formatting in April 4, 2014 will directly convert the Date string into a JavaScript Date object, such as: new Date ("2014-04-04T13: 04: 46.187 ") // The system may prompt Invalid Date new Date ("April 04, 2014") // The system may prompt that Invalid Date has been tested. For the same string, the system prompts Invalid Date during conversion on the company's computer, on the home computer again. Is it because it is related to the computer system environment? To be considered. Generally, there are only a few types of date formats. Why not use regular expressions? Start by thinking about it. The regular expression for date matching is as follows (Chinese supported):/^ ([0-9] {4 })[-/\. year] ([0-1]? [0-9] {1}) [-/\. month] ([0-3]? [0-9] {1}) [Day]?.? ([0-2]? [0-9] (: [0-6] [0-9]) {2 })? /If you input the previous date format, the result may be as follows. Some people may wonder why there are so many date groups? Useful later: the complete code is attached: copy the code function getDateStr (value, formatStr) {formatStr = formatStr | "yyyy-MM-dd "; // default format: var dateReg =/^ ([0-9] {4 })[-/\. year] ([0-1]? [0-9] {1}) [-/\. month] ([0-3]? [0-9] {1}) [Day]?.? ([0-2]? [0-9] (: [0-6] [0-9]) {2 })? /; Var arr = dateReg.exe c (value); if (arr & arr [0]) {if (formatStr) {var subReg =/y + (.) M + (.) d + (.)? /I; var subArr = subReg.exe c (formatStr); if (subArr) {var resStr = arr [1] + subArr [1]; resStr + = arr [2] + subArr [2]; resStr + = arr [3] + (subArr [3] | ""); return resStr ;}} return arr [0];} return "";} copy the Code call: var a1 = getDateStr ("21:36:01", "yyyy MM dd ") // April 17, 2014 var a2 = getDateStr ("2014-4-17T21: 36: 01") // default format Note: getDateStr does not support directly passing a javascript Date object parameter, but it is easy to expand, perform the value before conversion. And then call the javascript method in Date. I will not talk about it here.

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.