最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date.js parseExact() not parsing 4 digit years when passed in as an array - Stack Overflow

programmeradmin1浏览0评论

Am I missing something with Date.parseExact() in date.js? According to the api documentation, I should be able 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 in a string array which contains "...the expected format {String} or an array of expected formats {Array} of the date string." However, when I do this:

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

I get back nulls for dates containing 4 digit years (that is, matching the MMddyyyy and MM/dd/yyyy formats). Am I missing something or is this a bug in Date.js?

Here is the plete block of code, 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"));
     });

});

Am I missing something with Date.parseExact() in date.js? According to the api documentation, I should be able 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 in a string array which contains "...the expected format {String} or an array of expected formats {Array} of the date string." However, when I do this:

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

I get back nulls for dates containing 4 digit years (that is, matching the MMddyyyy and MM/dd/yyyy formats). Am I missing something or is this a bug in Date.js?

Here is the plete block of code, 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"));
     });

});
Share Improve this question edited Dec 19, 2011 at 22:02 seanicus asked Dec 19, 2011 at 20:58 seanicusseanicus 1,1715 gold badges20 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It appears date.js is attempting to parse the four-digit year as a two-digit year, failing, and returning null on failure.

To prevent this, switch your masks around so it tries 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/mblase75/ttEqh/1/

发布评论

评论列表(0)

  1. 暂无评论