Fel*_*ing 116
a = new Date(1995,11,17);
b = new Date(1995,11,17);
a.getTime() === b.getTime() // prints true
Run Code Online (Sandbox Code Playgroud)
Inc*_*tly 38
我用过这段代码:
Date.prototype.isSameDateAs = function(pDate) {
return (
this.getFullYear() === pDate.getFullYear() &&
this.getMonth() === pDate.getMonth() &&
this.getDate() === pDate.getDate()
);
}
Run Code Online (Sandbox Code Playgroud)
然后你就像这样称呼它: if (aDate.isSameDateAs(otherDate)) { ... }
cyb*_*bat 33
如果您只想检查日期是否在同一天发生,那么您可以使用该toDateString()
方法进行比较.此方法仅返回没有时间的日期:
var start = new Date('2015-01-28T10:00:00Z');
var end = new Date('2015-01-28T18:00:00Z');
if (start.toDateString() === end.toDateString()) {
// Same day - maybe different times
} else {
// Different day
}
Run Code Online (Sandbox Code Playgroud)
d2v*_*vid 13
键入convert to integers:
a = new Date(1995,11,17);
b = new Date(1995,11,17);
+a === +b; //true
Run Code Online (Sandbox Code Playgroud)
Hellnar酒店,
你可以尝试(原谅函数名称:) - 根据felix的valueof修改,而不是getTime)
function isEqual(startDate, endDate) {
return endDate.valueOf() == startDate.valueOf();
}
Run Code Online (Sandbox Code Playgroud)
用法:
if(isEqual(date1, date2)){
// do something
}
Run Code Online (Sandbox Code Playgroud)
可能会让你成为那里的一部分.
也可以看看:
'http://www.java2s.com/Tutorial/JavaScript/0240__Date/DatevalueOf.htm'
减去它们并与零比较:
var date1 = new Date();
var date2 = new Date();
Run Code Online (Sandbox Code Playgroud)
// 对日期做一些事情...
(date1 - date2) ? alert("not equal") : alert("equal");
Run Code Online (Sandbox Code Playgroud)
将其放入变量中:
var datesAreSame = !(date1 - date2);
Run Code Online (Sandbox Code Playgroud)
一个简单的单行替代方案,用于确定两个日期是否相等,忽略时间部分:
function isSameDate(a, b) {
return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}
Run Code Online (Sandbox Code Playgroud)
它确定日期 a 和 b 是否相差不超过一天并且共享一周中的同一天。
function isSameDate(a, b) {
return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
67762 次 |
最近记录: |