检查日期是否在范围内
我正在尝试检查mm.dd.yyyy格式的日期是否大于今天,并且小于今天的6个月后的日期。
I am trying to check if a date of format mm.dd.yyyy is greater than today and less than the date after 6 months from today.
这里是我的代码:
var isLinkExpiryDateWithinRange = function(value) {
var monthfield = value.split('.')[0];
var dayfield = value.split('.')[1];
var yearfield = value.split('.')[2];
var inputDate = new Date(yearfield, monthfield - 1, dayfield);
var today = new Date();
today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
alert(inputDate > today);//alert-> true
var endDate = today;
endDate.setMonth(endDate.getMonth() + 6);
alert(inputDate > today);//alert-> false
if(inputDate > today && inputDate < endDate) {
alert('1');
} else {
alert('2');/always alert it
}
}
如果我执行 isLinkExpiryDateWithinRange('12 .08.2012')
,我希望它显示1,因为它在范围内,但是它显示2。此外,第一个警报显示为true,第二个警报显示为false。
If I execute isLinkExpiryDateWithinRange('12.08.2012')
I wish it will show 1 as this is within the range, but it is displaying 2. Moreover the first alert is showing true and the second one false.
有人可以解释发生了什么吗?
Can anyone please explain what is happening?
更改:
var endDate = today;
至:
var endDate = new Date(today);
查看帖子通过引用或值值通过语言,以了解如何引用和更改对象。有一些非常好的例子可以帮助解释这个问题,尤其是:
See the posts here for how objects are referenced and changed. There are some really good examples that help explain the issue, notably:
相反,情况是传入的项目是按值传递的。
但是按值传递的项目本身就是引用。
Instead, the situation is that the item passed in is passed by value. But the item that is passed by value is itself a reference.