javascript 如何在Javascript中检查一个日期小于或等于另一个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27931432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check for one date is less than or equal another date in Javascript
提问by AJR
I am having a difficult time checking if one date is less than or equal to another.
我很难检查一个日期是否小于或等于另一个。
Here is my code,
这是我的代码,
var bftStartDt = input1[0]; //This is a string with value "01-Jul-2007"
var bftEndDt = input1[4]; //This is a string with value "01-Jul-1942"
var strtDt = new Date(bftStartDt);
var endDt = new Date(bftEndDt);
var flag = 0; // false
if (endDt <= strtDt){
flag = 1; // true
}
It never enters the if statement when it should ? What am I missing here.
它应该什么时候从不进入 if 语句?我在这里错过了什么。
Thanks
谢谢
回答by CognitiveDesire
回答by Chris Middleton
The problem here is that 01-Jul-2007
is not a format supported by the Date
object. Try doing 2007-01-07
instead. Then your program works as expected.
这里的问题是这01-Jul-2007
不是Date
对象支持的格式。尝试做2007-01-07
。然后你的程序按预期工作。
var bftStartDt = "01-07-2007"; //This is a string with value "01-Jul-2007"
var bftEndDt = "01-07-1942"; //This is a string with value "01-Jul-1942"
var strtDt = new Date(bftStartDt);
var endDt = new Date(bftEndDt);
var flag = 0; // false
if (endDt <= strtDt){
flag = 1; // true
}
if(flag === 1) {
console.log("It worked.");
}
According to MDN, the accepted formats are:
根据MDN,接受的格式是:
A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).
表示 RFC2822 或 ISO 8601 日期的字符串(可以使用其他格式,但结果可能出乎意料)。
So you could also use the format Jul 01 2007
. The full list of formats is in RFC 2822.
所以你也可以使用格式Jul 01 2007
。完整的格式列表在RFC 2822 中。