JavaScript 日期对象比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7606798/
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
JavaScript Date Object Comparison
提问by Harshana
When comparing date objects in Javascript I found that even comparing the same date does not return true.
在 Javascript 中比较日期对象时,我发现即使比较相同的日期也不会返回 true。
var startDate1 = new Date("02/10/2012");
var startDate2 = new Date("01/10/2012");
var startDate3 = new Date("01/10/2012");
alert(startDate1>startDate2); // true
alert(startDate2==startDate3); //false
How could I compare the equality of these dates? I am interested in utilizing the native Date
object of JS and not any third party libraries since its not appropriate to use a third party JS just to compare the dates.
我如何比较这些日期的相等性?我对利用Date
JS的本机对象而不是任何第三方库感兴趣,因为仅使用第三方 JS 来比较日期是不合适的。
回答by RobG
That is because in the second case, the actual date objects are compared, and two objects are never equal to each other. Coerce them to number:
那是因为在第二种情况下,比较实际的日期对象,并且两个对象永远不会彼此相等。强迫他们编号:
alert( +startDate2 == +startDate3 ); // true
If you want a more explicity conversion to number, use either:
如果您想要更明确地转换为数字,请使用:
alert( startDate2.getTime() == startDate3.getTime() ); // true
or
或者
alert( Number(startDate2) == Number(startDate3) ); // true
Oh, a reference to the spec: §11.9.3 The Abstract Equality Comparison Algorithmwhich basically says when comparing objects, obj1 == obj2
is true only if they refer to the same object, otherwise the result is false.
哦,参考规范:§11.9.3抽象平等比较算法,它基本上说在比较对象时,obj1 == obj2
只有当它们引用同一个对象时才为真,否则结果为假。
回答by Tomasz Nurkiewicz
Compare dates using getTime()
returning number of milliseconds from epoch (i.e. a number):
使用getTime()
从纪元返回的毫秒数(即一个数字)来比较日期:
var startDate1 = new Date("02/10/2012");
var startDate2 = new Date("01/10/2012");
var startDate3 = new Date("01/10/2012");
alert(startDate1.getTime() > startDate2.getTime()); // true
alert(startDate2.getTime() == startDate3.getTime()); //true
Also consider using Date
constructor taking explicit year/month/date number rather then relying on string representation (see: Date.parse()). And remember that dates in JavaScript are always represented using client (browser) timezone.
还可以考虑使用Date
采用显式年/月/日期数字的构造函数,而不是依赖字符串表示(请参阅:Date.parse())。请记住,JavaScript 中的日期始终使用客户端(浏览器)时区表示。
回答by kennebec
You do not need to use the getTime method- you can subtract a date object from another date object. It will return the milliseconds difference(negative, if the second is a later date)
您不需要使用 getTime 方法 - 您可以从另一个日期对象中减去一个日期对象。它将返回毫秒差异(负,如果第二个是较晚的日期)
var startDate1 = new Date("02/10/2012");
var startDate2 = new Date("01/10/2012");
var diff= (startDate1 -startDate2)
// evaluates to 0 if the dates have the same timestamp
// 如果日期具有相同的时间戳,则计算结果为 0
回答by gion_13
you can compare the actual milliseconds :
您可以比较实际的毫秒数:
alert(startDate2.getTime() === startDate3.getTime());
回答by PeteBaser
You can also use the function valueOf()
您还可以使用函数 valueOf()
var startDate1 = new Date("02/10/2012").valueOf();
var startDate2 = new Date("01/10/2012").valueOf();
var startDate3 = new Date("01/10/2012").valueOf();
alert(startDate1>startDate2); // 1326150000000 > 1328828400000 true
alert(startDate2==startDate3); // 1328828400000 > 1326150000000 false