Date.js parseExact()作为数组传入时不解析4位数字的年份 [英] Date.js parseExact() not parsing 4 digit years when passed in as an array
问题描述
我在date.js中缺少带有Date.parseExact()的东西吗?根据api文档,我应该能够做到这一点:
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
也就是说,我应该能够传递一个字符串数组,其中包含"...日期字符串的预期格式{String}或预期格式{Array}的数组."但是,当我这样做时:
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"])
对于包含4位数字年份的日期,我会返回空值(即,与MMddyyyyy和MM/dd/yyyy格式匹配).我是否缺少某些东西,或者这是Date.js中的错误?
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 complete 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"));
});
});
推荐答案
看来date.js试图将四位数的年份解析为两位数的年份,但失败,并在失败时返回null.
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.net/mblase75/ttEqh/1/
这篇关于Date.js parseExact()作为数组传入时不解析4位数字的年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!