Date.js parseExact () does not handle 4-digit years when passed as an array

Am I missing something with Date.parseExact () in date.js? According to the api documentation, I have to do this:

Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]); // The Date of 15-Oct-2004 

That is, I should be able to pass an array of strings containing "... the expected format {String} or an array of expected formats {Array} date strings." However, when I do this:

 var d = Date.parseExact($(this).val(), ["MMddyy", "Mddyyyy", "MM/dd/yy","MM/dd/yyyy"]) 

I return zeros for dates containing 4-digit years (that is, corresponding to the formats MMddyyyy and MM / dd / yyyy). Am I missing something or is it an error in Date.js?

Here is the complete code block for context:

 $(function () { $('#FCSaleDate').change(function (e) { var d = Date.parseExact($(this).val(), ["MMddyy", "MMddyyyy", "MM/dd/yy","MM/dd/yyyy"]) alert(d.toString("MM/dd/yyyy")); }); }); 
+4
source share
1 answer

It looks like date.js is trying to parse a four-digit year as a two-digit year, unsuccessful, and return null on error.

To prevent this, enable the masks to try the four-digit masks first:

 $(function () { $('#FCSaleDate').change(function (e) { var d = Date.parseExact($(this).val(),["MMddyyyy","MMddyy","M/d/yyyy","M/d/yy"]); alert(d.toString("MM/dd/yyyy")); }); }); 

http://jsfiddle.net/mblase75/ttEqh/1/

+4
source

Source: https://habr.com/ru/post/1387096/


All Articles