The way JavaScript converts dates to strings by date-mask

Source: Internet
Author: User
Tags join tostring

This example describes how JavaScript translates dates into strings by date-mask. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64-65 var monthnames = ["January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "Nove Mber "," December "]; var daynames = ["Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saturday"]; var shortmths = ["No More", "Feb", "Mar", "APR", "may", "June", "O", "Aug", "Sep", "Oct", "Nov", "Dec"]; var shortdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; var stringtodate = function (Sdate, Sformat, cutOff) {//Input:a date value as a string, it ' s format as a string e.g. ' d D-mmm-yy '//optional:a cutoff (integer) for 2 digit years. If no ' d ' appears in the format string then the 1st of the month is assumed. If the cut-off is then the value would be converted//to 2020; If the then this would be converted to 1940. If no cut-off is supplied then ' would be pre-pended to the year (YY). Output:a string in the format ' yyyy/mm/dd ' or '/won't be attempt to convert certain combinations e.g. DMM, MDD, DDM, Yyyyd. var sparsed, Fndsingle; Sparsed would be constructed in the format ' yyyy/mm/dd ' sdate = sdate.tostring (). toUpperCase (); Sformat = Sformat.touppercase (); if (Sformat.search (/mmmm| mmm/) + 1) {//replace mar/march with, etc. sdate = sdate.replace (new RegExp (' + shortmths.join (' | ') + ') [a-z]* ', ' g I '), function (m) {var i = shortmths.indexof (M.charat (0). toUpperCase () + M.SUBSTR (1, 2). toLowerCase ()) + 1; return ((I &L T 10)? "0" + I: "" + i). toString (); Sformat = Sformat.replace (/mmmm| mmm/g, ' MM '); } if (Sformat.search (/dddd| ddd/) + 1) {//replace Tue/tuesday, etc. with ' Sdate = Sdate.replace (new RegExp (' + shortdays.join (' | ') + ') [a-z]* ', ' GI '), '); Sformat = Sformat.replace (/dddd| Ddd/g, ""); } sdate = Sdate.replace (/(^| d) (d) (=d|$)/g, function ($, $, $) {//single digits 2 with 0 ' + $}); Sformat = Sformat.replace (/(^|[ ^dmy]) (d| M) (=[^dmy]|$)/g, function ($, $) {return $ + $ + $//replace D or M with DD and MM}); ARe there still single Ds or Ms? Fndsingle = Sformat.search (/(^|[ ^D]) D ([^d]|$) | (^| [^m]) M ([^m]|$)/) +1; Don't attempt to parse, for example, ' DMM ' if (fndsingle) return '; Sformat = Sformat.replace (/(^|[ ^y]) (YY) (? =[^y]|$)/g, function ($, $, index) {var tempdate = sdate.substr (0, index + 1); tempdate = = (CUTOFF)? ((parseint (SDATE.SUBSTR (index + 1, 2), ten) > CutOff)? ' 19 ': ' 20 '): ' 20 '; Tempdate + = SDATE.SUBSTR (index + 1); Sdate = tempdate; Return $ + $ + $; }); sparsed = (' Yyyy/mm/dd '). Replace (/yyyy| mm| dd/g, function (m) {return (Sformat.indexof (m) + 1)? Sdate.substr (Sformat.indexof (M), m.length): ";}"; if (Sparsed.charat (0) = = '/') {//If no specified, assume the current year sparsed = (new Date (). getFullYear ()) + SP arsed; } if (Sparsed.charat (sparsed.length-1) = = '/') {//If no date, assume the 1st of month sparsed + + ';}//should End up with characters ... return (Sparsed.length = 10)? sparsed: '; };

The

wants this article to help you with your JavaScript programming.

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.