Javascript 如何检查两个日期是否不在同一日历日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12802722/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:29:44  来源:igfitidea点击:

How to check if two dates not on the same calendar day

javascriptdate

提问by Andreas K?berle

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;

回答by Stu Stein

Another option is using .toDateString()function to parse both dates into strings. The function formats output to: "Wed Jul 28 1993." Then you can compare both date strings.

另一种选择是使用.toDateString()函数将两个日期解析为字符串。该函数将输出格式设置为:“Wed Jul 28 1993”。然后您可以比较两个日期字符串。

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

回答by Jamiec

How about checking for the same day, month & year:

如何检查同一天、同一月和同一年:

var isSameDay = (dateToCheck.getDate() == actualDate.getDate() 
        && dateToCheck.getMonth() == actualDate.getMonth()
        && dateToCheck.getFullYear() == actualDate.getFullYear())

回答by Niet the Dark Absol

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 和闰年时不会遇到奇怪的情况,否则可能会干扰。

回答by David

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();

回答by sada khanpit

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);

回答by Web_Designer

Use a startOfmethod for instances of Date:

使用startOf以下实例的方法Date

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;
};

回答by tkone

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 Dateobject (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(官方版本 159779)测试版上),如果你不通过时间,它们将在午夜生成。

If you're getting a time, just drop it on the floor and test against midnight.

如果您有时间,只需将其放在地板上并在午夜进行测试。