0

我正在使用 jquery 的 daterangepicker 组件。当我将日期传递为“2011 年 1 月”时,jquery 的 Date.parse() 函数会给出一些奇怪的输出。以下是不同用例的示例输出:

代码片段:

var dateStr = "Jan 2011";
var dt = Date.parse(dateStr);
alert(dt.getDate() + "/" + dt.getMonth());

输出 :

dateStr = "Jan 2011"  : output = 25/0 (todays date is 25 feb 2011)
dateStr = "Feb 2011"  : output = 1/1
dateStr = "Dec 2011"  : output = 1/11

因此,对于一月的任何其他月份,日期设置为该月的第一天,而仅对于一月,日期设置为当前日期。

知道这可能是什么原因吗?

4

2 回答 2

0

在您的示例中,这不是标准Date.parse功能。
Standartparse函数返回数字结果,而不是Date对象。
Javascript parse() 方法

在您的示例parse函数中返回Date对象。
函数Date.parse在您的代码中重新定义。请检查一下。

在您的情况下, Standartparse函数始终返回每月的第一天

于 2013-04-24T15:03:52.353 回答
0

function myFunction() {
    var d = Date.parse("Jan 2011");
    document.getElementById("demo").innerHTML = d;
}
<!DOCTYPE html>
<html>
<body>

<p>Click the button to display milliseconds between a specified date and Jan 2011.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

</body>
</html>

parse() 方法解析日期字符串并返回日期字符串与 2011 年 1 月午夜之间的毫秒数

于 2017-07-28T10:18:41.083 回答