Javascript 检查两个日期是否具有相同的日期信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4428327/
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
Checking if two Dates have the same date info
提问by Hellnar
Greetings,
你好,
How can I check if two different date objects have the same date information(having same day, month, year ...)? I have tried "==", "===" and .equals but none seems to work.
如何检查两个不同的日期对象是否具有相同的日期信息(具有相同的日、月、年...)?我尝试过“==”、“===”和 .equals,但似乎都不起作用。
回答by Felix Kling
回答by Incidently
I used this code:
我使用了这个代码:
Date.prototype.isSameDateAs = function(pDate) {
return (
this.getFullYear() === pDate.getFullYear() &&
this.getMonth() === pDate.getMonth() &&
this.getDate() === pDate.getDate()
);
}
Then you just call it like : if (aDate.isSameDateAs(otherDate)) { ... }
然后你就可以这样称呼它: if (aDate.isSameDateAs(otherDate)) { ... }
回答by cyberwombat
If you are only interested in checking if dates occur on the same day regardless of time then you can use the toDateString()
methodto compare. This method returns only the date without time:
如果您只想检查日期是否在同一天发生而不管时间如何,那么您可以使用该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
}
回答by d2vid
Type convert to integers:
类型转换为整数:
a = new Date(1995,11,17);
b = new Date(1995,11,17);
+a === +b; //true
回答by jim tollan
Hellnar,
赫尔纳尔,
you could try (pardon the function name :) - amended per felix's valueof, rather than getTime)
您可以尝试(请原谅函数名称 :) - 根据 felix 的 valueof 进行修改,而不是 getTime)
function isEqual(startDate, endDate) {
return endDate.valueOf() == startDate.valueOf();
}
usage:
用法:
if(isEqual(date1, date2)){
// do something
}
might get you part of the way there.
可能会让你参与其中。
see also:
也可以看看:
'http://www.java2s.com/Tutorial/JavaScript/0240__Date/DatevalueOf.htm'
'http://www.java2s.com/Tutorial/JavaScript/0240__Date/DatevalueOf.htm'
回答by que1326
For better date support use moment.jsand isSamemethod
为了获得更好的日期支持,请使用moment.js和isSame方法
var starDate = moment('2018-03-06').startOf('day');
var endDate = moment('2018-04-06').startOf('day');
console.log(starDate.isSame(endDate)); // false ( month is different )
var starDate = moment('2018-03-06').startOf('day');
var endDate = moment('2018-03-06').startOf('day');
console.log(starDate.isSame(endDate)); // true ( year, month and day are the same )
回答by Ege ?zcan
subtract them and compare to zero:
减去它们并与零比较:
var date1 = new Date();
var date2 = new Date();
// do something with the dates...
// 对日期做一些事情...
(date1 - date2) ? alert("not equal") : alert("equal");
to put it into a variable:
将其放入变量中:
var datesAreSame = !(date1 - date2);
回答by Philip Bijker
A simple single line alternative for determining if two dates are equal, ignoring the time part:
一个简单的单行替代方法,用于确定两个日期是否相等,忽略时间部分:
function isSameDate(a, b) {
return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}
It determines if dates a and b differ no more than one day and share the same day of the week.
它确定日期 a 和 b 是否相差不超过一天并共享一周中的同一天。
function isSameDate(a, b) {
return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}
console.log(isSameDate(new Date(2017, 7, 21), new Date(2017, 7, 21))); //exact same date => true
console.log(isSameDate(new Date(2017, 7, 21, 23, 59, 59), new Date(2017, 7, 21))); //furthest same dates => true
console.log(isSameDate(new Date(2017, 7, 20, 23, 59, 59), new Date(2017, 7, 21))); //nearest different dates => false
console.log(isSameDate(new Date(2016, 7, 21), new Date(2017, 7, 21))); //different year => false
console.log(isSameDate(new Date(2017, 8, 21), new Date(2017, 7, 21))); //different month => false