[英]How to check if two dates not on the same calendar day
How to check if two dates not on the same day. 如何检查两个日期是否在同一天。 I came up with this solution but maybe there is a better way to do this:
我提出了这个解决方案,但也许有更好的方法来做到这一点:
var actualDate = new Date();
var isNotToday = dateToCheck.getDay() !== actualDate.getDay() || dateToCheck < actualDate - 24 * 60 * 60 * 1000;
Another option is using .toDateString()
function to parse both dates into strings. 另一种选择是使用
.toDateString()
函数将两个日期解析为字符串。 The function formats output to: "Wed Jul 28 1993." 函数格式输出到:“Wed Wed 28 1993”。 Then you can compare both date strings.
然后你可以比较两个日期字符串。
actualDate.toDateString() === dateToCheck.toDateString()
// returns true if actualDate is same day as dateToCheck
Here's a Plunker: 这是一个Plunker:
http://plnkr.co/edit/J5Dyn78TdDUzX82T0ypA http://plnkr.co/edit/J5Dyn78TdDUzX82T0ypA
And more from MDN: 还有更多来自MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString
How about checking for the same day, month & year: 如何检查同一天,一个月和一年:
var isSameDay = (dateToCheck.getDate() == actualDate.getDate()
&& dateToCheck.getMonth() == actualDate.getMonth()
&& dateToCheck.getFullYear() == actualDate.getFullYear())
It would be safest to check day, month and year: 检查日,月和年最安全:
var isNotToday = dateToCheck.getDate() != actualDate.getDate()
|| dateToCheck.getMonth() != actualDate.getMonth()
|| dateToCheck.getFullYear() != actualDate.getFullYear();
This will ensure you don't get oddities when it comes to DST and leap years which could interfere otherwise. 这将确保您在DST和闰年时不会产生奇怪的现象,否则可能会干扰。
function getStartOfDay(aDate){
//returns the very beginning of the same day
return new Date(aDate.getTime() - aDate.getTime() % 86400000);
}
var aDate1 = getStartOfDay(new Date());
var aDate2 = getStartOfDay(new Date(1981, 7, 3));
var result = aDate1.getTime() === aDate2.getTime();
Don't create the date object with a time so they're both generated at midnight. 不要用时间创建日期对象,因此它们都是在午夜生成的。 Then just check the epoch time.
然后查看纪元时间。
new Date('11/12/2012').getTime() === new Date('11/11/2012').getTime()
> false
new Date('11/12/2012').getTime() === new Date('11/12/2012').getTime()
> true
new Date('November 12, 2012').getTime() === new Date('11/12/2012').getTime()
> true
new Date('12 November, 2012').getTime() === new Date('11/12/2012').getTime()
> true
The dates just need to be parseable by the Date
object (I'm on chrome 23.0.1271.17 (Official Build 159779) beta) and if you don't pass a time they'll generate at midnight. 日期只需要由
Date
对象解析(我在chrome 23.0.1271.17(Official Build 159779)beta),如果你没有通过他们将在午夜生成的时间。
If you're getting a time, just drop it on the floor and test against midnight. 如果你有时间,只需将它放在地板上并在午夜时间进行测试。
function isEqual(startDate, endDate) { return endDate.valueOf() == startDate.valueOf(); } var now =new Date(); var startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate()); var endDate = new Date(2017, 0, 13) var result = isEqual(startDate , endDate); console.log(result);
Use a startOf
method for instances of Date
: 对
Date
实例使用startOf
方法:
dateA.startOf('day').getTime() === dateB.startOf('day').getTime()
Here's the method: 这是方法:
Date.prototype.startOf = function(unit) {
var clone = new Date(this.getTime()), day;
/* */if (unit === 'second') clone.setMilliseconds(0);
else if (unit === 'minute') clone.setSeconds(0,0);
else if (unit === 'hour' ) clone.setMinutes(0,0,0);
else {
clone.setHours(0,0,0,0);
if (unit === 'week') {
day = clone.getDay();
clone = day ? new Date(clone - 1000 * 60 * 60 * 24 * day) : clone;
}
else if (unit === 'month') clone.setDate(1);
else if (unit === 'year' ) clone.setMonth(0,1);
}
return clone;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.