我正在做一个应用程序,我遇到了这个问题,我认为比较日期会很容易,但事实并非如此。如果日期是同一年,我在 if
中的条件运行良好,但如果值类似于下面给出的值,则结果不会变为 true
应该为 false
。那么我该如何正确比较我拥有的这种日期格式呢?
startDate.toLocaleDateString() 的值为 12/24/2016
endDate.toLocaleDateString() 的值为 01/03/2017
if(startDate.toLocaleDateString() > endDate.toLocaleDateString())
{
//do this
}
else
{
//do this
}
最佳答案
你可以比较Date
直接,因为它使用 EPOCH time ,具有可比性。
Creates a JavaScript
Date
instance that represents a single moment in time.Date
objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.
if (startDate > endDate) {
它使用 Date#valueOf
The
valueOf()
method returns the primitive value of a Date object.
返回
The number of milliseconds between 1 January 1970 00:00:00 UTC and the given date.
关于javascript - 比较/条件语句 toLocaleDateString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41869656/