在 JavaScript 中仅比较日期部分而不比较时间

Comparing date part only without comparing time in JavaScript

提问人:moleculezz 提问时间:4/23/2010 最后编辑:Peter Mortensenmoleculezz 更新时间:7/27/2023 访问量:653847

问:

下面的代码有什么问题?

也许只比较日期而不是时间会更简单。我也不确定该怎么做,我搜索了一下,但我找不到我的确切问题。

顺便说一句,当我在警报中显示两个日期时,它们显示完全相同。

我的代码:

window.addEvent('domready', function() {
    var now = new Date();
    var input = $('datum').getValue();
    var dateArray = input.split('/');
    var userMonth = parseInt(dateArray[1])-1;
    var userDate = new Date();
    userDate.setFullYear(dateArray[2], userMonth, dateArray[0], now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());

    if (userDate > now)
    {
        alert(now + '\n' + userDate);
    }
});

有没有更简单的方法来比较日期而不包括时间?

JavaScript 日期 比较 MooTools

评论

7赞 Naveen 5/28/2021
date1.toDateString() === date2.toDateString()
0赞 Cheshiremoe 10/13/2021
死而复生:在我得到月份增加 1 的日期使用 toSting 时,我得到了破碎的结果。在FireFox和Edge的此页面链接上进行了测试。date.setFullYear(yyyy,mm,dd)

答:

2赞 Diodeus - James MacFarlane 4/23/2010 #1

date.js 库对于这些事情很方便。它使所有与 JS 日期相关的 scriping 变得更加容易。

评论

18赞 Mahmoud Hanafy 6/28/2016
您能否添加更多有关此库如何使此过程变得更容易的信息?
1赞 Alex K. 4/23/2010 #2

确保使用 4 位数的年份构造为 。userDatesetFullYear(10, ...) !== setFullYear(2010, ...)

940赞 nexar 6/1/2011 #3

我仍在学习 JavaScript,我发现唯一适合我比较两个没有时间的日期的方法是使用 Date 对象的 setHours 方法并将小时、分钟、秒和毫秒设置为零。然后比较两个日期。

例如

date1 = new Date()
date2 = new Date(2011,8,20)

date2将小时、分钟、秒和毫秒设置为零,但 date1 会将它们设置为创建 date1 的时间。要删除 date1 上的小时、分钟、秒和毫秒,请执行以下操作:

date1.setHours(0,0,0,0)

现在,您可以仅将两个日期作为 DATES 进行比较,而不必担心时间元素。

评论

85赞 Erwin Wessels 5/15/2013
请注意,测试似乎不能提供一致的行为;最好做,甚至.陌生 感。date1 === date2date1.valueOf() === b.valueOf()date1.getTime() === date2.getTime()
7赞 Oliver 2/8/2015
注意:如果 date1 和 date2 分别在冬季和夏季,并且您计划使用 addDays(1) 从一个迭代到另一个,那么问题在于由于夏令时,它们不会具有相同的时区,因此应该给出相等日期的最后一个比较将不起作用,因为这两个日期实际上不是 00:00:00:0。
13赞 Adam 8/14/2015
我知道这是一个老问题,但在搜索时,它首先出现在谷歌中。请小心这个答案。如果用户与日期对象的创建者不在同一时区,这将给出不正确的答案。例如,将计算机操作系统上的时区更改为东海岸(美国)时间。打开浏览器的控制台,然后键入 。现在将操作系统的时区更改为太平洋时间(美国)。在相同的浏览器控制台中,键入,您将返回,而不是 20 日星期二!var date2 = new Date(2011,8,20)date2.toDateString()Mon Sep 19 2011
11赞 Sang 2/29/2016
另请注意,根据浏览器自动检测到的当前时区设置时间。尝试:将打印,日期 28,但不是 29 我当前的时区是setHours()t = new Date("2016-02-29T01:45:49.098Z");t.setHours(0,0,0,0);console.log(t.toJSON());"2016-02-28T15:00:00.000Z"Asia/Tokyo
11赞 Stijn de Witt 9/14/2017
这个答案确实应该更新为使用方法而不是本地/时区感知变体。setUTCxxx
9赞 Useless Code 6/2/2011 #4

这可能是一个更干净的版本,另请注意,在使用 parseInt 时应始终使用基数。

window.addEvent('domready', function() {
    // Create a Date object set to midnight on today's date
    var today = new Date((new Date()).setHours(0, 0, 0, 0)),
    input = $('datum').getValue(),
    dateArray = input.split('/'),
    // Always specify a radix with parseInt(), setting the radix to 10 ensures that
    // the number is interpreted as a decimal.  It is particularly important with
    // dates, if the user had entered '09' for the month and you don't use a
    // radix '09' is interpreted as an octal number and parseInt would return 0, not 9!
    userMonth = parseInt(dateArray[1], 10) - 1,
    // Create a Date object set to midnight on the day the user specified
    userDate = new Date(dateArray[2], userMonth, dateArray[0], 0, 0, 0, 0);

    // Convert date objects to milliseconds and compare
    if(userDate.getTime() > today.getTime())
    {
            alert(today+'\n'+userDate);
    }
});

请查看 MDC parseInt 页面,了解有关基数的详细信息。

JSLint 是一个很好的工具,可以捕获诸如缺少基数之类的东西,以及许多其他可能导致晦涩难辨错误的东西。它迫使您使用更好的编码标准,以避免将来的麻烦。我在我编写的每个 JavaScript 项目中都使用它。

评论

0赞 Useless Code 7/26/2016
@EmKay 现在这应该不是一个大问题,因为 ES5 在大多数浏览器中都是相当标准的,它禁止八进制解释,但出于向后兼容性的原因,某些浏览器可能仍在使用旧的八进制解释,所以我会在可预见的未来继续使用基数。
0赞 Useless Code 7/26/2016
如果使用严格模式,则八进制解释将引发错误,这是一件好事,因为它可以帮助您找到可能被误解的错误。
2赞 Fabrizio 11/7/2011 #5

这是我的做法:

var myDate  = new Date($('input[name=frequency_start]').val()).setHours(0,0,0,0);
var today   = new Date().setHours(0,0,0,0);
if(today>myDate){
    jAlert('Please Enter a date in the future','Date Start Error', function(){
        $('input[name=frequency_start]').focus().select();
    });
}

评论

0赞 Fabrizio 5/8/2013
I currently use this in production and I do not have any issue or browser incompatibility etc... any reasons why you are saying that is required ?getTime()
5赞 Timothy Walters 4/14/2014
Please be aware that modifies the object it is called on, and returns the date as a number of milliseconds (equivalent to calling ). Therefore your variable is not a object like some would expect, but is actually an integer number of milliseconds. As a side-effect this is why you didn't need to call before comparing, since you already have in an obscure manner.setHours()getTime()todayDategetTime()
2赞 Aleks 1/21/2014 #6

After reading this question quite same time after it is posted I have decided to post another solution, as I didn't find it that quite satisfactory, at least to my needs:

I have used something like this:

var currentDate= new Date().setHours(0,0,0,0);

var startDay = new Date(currentDate - 86400000 * 2);
var finalDay = new Date(currentDate + 86400000 * 2);

In that way I could have used the dates in the format I wanted for processing afterwards. But this was only for my need, but I have decided to post it anyway, maybe it will help someone

评论

1赞 Timothy Walters 4/14/2014
Please note your variable isn't a date, it's a number of milliseconds since 1970-01-01. The method modifies the date object it is called on, and returns the equivalent of (value of date in milliseconds since 1970-01-01).currentDatesetHours()getTime()
4赞 Max Yari 3/26/2015 #7

As I don't see here similar approach, and I'm not enjoying setting h/m/s/ms to 0, as it can cause problems with accurate transition to local time zone with changed object (I presume so), let me introduce here this, written few moments ago, lil function:date

+: Easy to use, makes a basic comparison operations done (comparing day, month and year without time.)
: It seems that this is a complete opposite of "out of the box" thinking.
-

function datecompare(date1, sign, date2) {
    var day1 = date1.getDate();
    var mon1 = date1.getMonth();
    var year1 = date1.getFullYear();
    var day2 = date2.getDate();
    var mon2 = date2.getMonth();
    var year2 = date2.getFullYear();
    if (sign === '===') {
        if (day1 === day2 && mon1 === mon2 && year1 === year2) return true;
        else return false;
    }
    else if (sign === '>') {
        if (year1 > year2) return true;
        else if (year1 === year2 && mon1 > mon2) return true;
        else if (year1 === year2 && mon1 === mon2 && day1 > day2) return true;
        else return false;
    }    
}

Usage:

datecompare(date1, '===', date2) for equality check,
for greater check,
for less or equal check
datecompare(date1, '>', date2)!datecompare(date1, '>', date2)

Also, obviously, you can switch and in places to achieve any other simple comparison.date1date2

评论

0赞 edencorbin 2/12/2016
Nice one, plus plus, helped me out, now hopefully the iso conversions don't bite, haha, thx.
101赞 AdEpt 4/13/2015 #8

How about this?

Date.prototype.withoutTime = function () {
    var d = new Date(this);
    d.setHours(0, 0, 0, 0);
    return d;
}

It allows you to compare the date part of the date like this without affecting the value of your variable:

var date1 = new Date(2014,1,1);
new Date().withoutTime() > date1.withoutTime(); // true

评论

0赞 DanimalReks 5/11/2020
This is by far the most elegant answer to the original post. I did not create a separate function for my purposes, just included the two lines of code.
4赞 andrey.shedko 12/8/2020
Add custom functionality to the prototype is not so good idea.
4赞 Grant 4/6/2022
Why @andrey.shedko? Saying something is a bad idea or is the wrong way without your reasoning is bad for everyone. If it works and is something you re-use throughout your project and can save you repeated manual pruning it works justt fine and is fine to use.
0赞 freedomn-m 6/14/2023
As noted in the accepted answer that uses , this will give the wrong time if you have a timezone other than UTC/GMT. The prototype could be updated to use the method.setHourswithoutTimeDate.UTC
1赞 Miguel 1/27/2016 #9

You can use some arithmetic with the total of ms.

var date = new Date(date1);
date.setHours(0, 0, 0, 0);

var diff = date2.getTime() - date.getTime();
return diff >= 0 && diff < 86400000;

I like this because no updates to the original dates are made and perfom faster than string split and compare.

Hope this help!

248赞 bbsimonbb 6/27/2016 #10

BEWARE THE TIMEZONE

Using the date object to represent just-a-date straight away gets you into a huge excess precision problem. You need to manage time and timezone to keep them out, and they can sneak back in at any step. The accepted answer to this question falls into the trap.

A javascript date has no notion of timezone. It's a moment in time (ticks since the epoch) with handy (static) functions for translating to and from strings, using by default the "local" timezone of the device, or, if specified, UTC or another timezone. To represent just-a-date™ with a date object, you want your dates to represent UTC midnight at the start of the date in question. This is a common and necessary convention that lets you work with dates regardless of the season or timezone of their creation. So you need to be very vigilant to manage the notion of timezone, both when you create your midnight UTC Date object, and when you serialize it.

Lots of folks are confused by the default behaviour of the console. If you spray a date to the console, the output you see will include your timezone. This is just because the console calls on your date, and gives you a local represenation. The underlying date has no timezone! (So long as the time matches the timezone offset, you still have a midnight UTC date object)toString()toString()

Deserializing (or creating midnight UTC Date objects)

This is the rounding step, with the trick that there are two "right" answers. Most of the time, you will want your date to reflect the local timezone of the user. What's the date here where I am.. Users in NZ and US can click at the same time and usually get different dates. In that case, do this...

// create a date (utc midnight) reflecting the value of myDate and the environment's timezone offset.
new Date(Date.UTC(myDate.getFullYear(),myDate.getMonth(), myDate.getDate()));

Sometimes, international comparability trumps local accuracy. In that case, do this...

// the date in London of a moment in time. Device timezone is ignored.
new Date(Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate()));

Deserialize a date

Often dates on the wire will be in the format YYYY-MM-DD. To deserialize them, do this...

var midnightUTCDate = new Date( dateString + 'T00:00:00Z');

Serializing

Having taken care to manage timezone when you create, you now need to be sure to keep timezone out when you convert back to a string representation. So you can safely use...

  • toISOString()
  • getUTCxxx()
  • getTime() //returns a number with no time or timezone.
  • .toLocaleDateString("fr",{timeZone:"UTC"}) // whatever locale you want, but ALWAYS UTC.

And totally avoid everything else, especially...

  • getYear(),,getMonth()getDate()

So to answer your question, 7 years too late...

<input type="date" onchange="isInPast(event)">
<script>
var isInPast = function(event){
  var userEntered = new Date(event.target.valueAsNumber); // valueAsNumber has no time or timezone!
  var now = new Date();
  var today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() ));
  if(userEntered.getTime() < today.getTime())
    alert("date is past");
  else if(userEntered.getTime() == today.getTime())
    alert("date is today");
  else
    alert("date is future");

}
</script>

See it running...

Update 2022... free stuff with tests ...

The code below is now an npm package, Epoq. The code is on github. You're welcome :-)

Update 2019... free stuff...

Given the popularity of this answer, I've put it all in code. The following function returns a wrapped date object, and only exposes those functions that are safe to use with just-a-date™.

Call it with a Date object and it will resolve to JustADate reflecting the timezone of the user. Call it with a string: if the string is an ISO 8601 with timezone specified, we'll just round off the time part. If timezone is not specified, we'll convert it to a date reflecting the local timezone, just as for date objects.

function JustADate(initDate){
  var utcMidnightDateObj = null
  // if no date supplied, use Now.
  if(!initDate)
    initDate = new Date();

  // if initDate specifies a timezone offset, or is already UTC, just keep the date part, reflecting the date _in that timezone_
  if(typeof initDate === "string" && initDate.match(/(-\d\d|(\+|-)\d{2}:\d{2}|Z)$/gm)){  
     utcMidnightDateObj = new Date( initDate.substring(0,10) + 'T00:00:00Z');
  } else {
    // if init date is not already a date object, feed it to the date constructor.
    if(!(initDate instanceof Date))
      initDate = new Date(initDate);
      // Vital Step! Strip time part. Create UTC midnight dateObj according to local timezone.
      utcMidnightDateObj = new Date(Date.UTC(initDate.getFullYear(),initDate.getMonth(), initDate.getDate()));
  }

  return {
    toISOString:()=>utcMidnightDateObj.toISOString(),
    getUTCDate:()=>utcMidnightDateObj.getUTCDate(),
    getUTCDay:()=>utcMidnightDateObj.getUTCDay(),
    getUTCFullYear:()=>utcMidnightDateObj.getUTCFullYear(),
    getUTCMonth:()=>utcMidnightDateObj.getUTCMonth(),
    setUTCDate:(arg)=>utcMidnightDateObj.setUTCDate(arg),
    setUTCFullYear:(arg)=>utcMidnightDateObj.setUTCFullYear(arg),
    setUTCMonth:(arg)=>utcMidnightDateObj.setUTCMonth(arg),
    addDays:(days)=>{
      utcMidnightDateObj.setUTCDate(utcMidnightDateObj.getUTCDate + days)
    },
    toString:()=>utcMidnightDateObj.toString(),
    toLocaleDateString:(locale,options)=>{
      options = options || {};
      options.timeZone = "UTC";
      locale = locale || "en-EN";
      return utcMidnightDateObj.toLocaleDateString(locale,options)
    }
  }
}


// if initDate already has a timezone, we'll just use the date part directly
console.log(JustADate('1963-11-22T12:30:00-06:00').toLocaleDateString())
// Test case from @prototype's comment
console.log("@prototype's issue fixed... " + JustADate('1963-11-22').toLocaleDateString())

评论

5赞 Stijn de Witt 9/14/2017
Your answer contains really valuable info! Unfortunately you did not actually add the answer to OP's actual question, preventing me from upvoting it. Try adding in a small code snippet that answers OP's question.
2赞 Stijn de Witt 9/15/2017
Ha ha ha @ '7 years too late'! I like your style user1585345! You got my vote!
0赞 RikRak 5/15/2018
I found this to be helpful. Note there is a typo in the " international comparability" snippet. I think it should be: new Date(Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate()));
0赞 prototype 12/30/2020
Note that this wraps a date+time the time set to just past the stroke of midnight UTC, and that date+time can be interpreted differently by locale. For instance, returns which is the prior date in time zones west of the Atlantic. So if the goal is to interpret a date string to display the same calendar date in another format, instead of wrapping a set to UTC, it might be better to modify wrap an set to SST (American Samoa Time) instead, so returns the same calendar date in (almost) all locales.JustADate('1963-11-22').toLocaleDateString()"11/21/1963"DatesstMidnightDateObj
0赞 bbsimonbb 8/8/2022
@prototype your issue was due to the fact that new Date("yyyy-MM-dd") does actually create a midnight UTC date object, (At least in Chrome. It may be implementation dependant). This was not well managed in my code, but it's fixed now.
29赞 Joshua Pinter 7/11/2016 #11

Using Moment.js

If you have the option of including a third-party library, it's definitely worth taking a look at Moment.js. It makes working with and much, much easier.DateDateTime

For example, seeing if one Date comes after another Date but excluding their times, you would do something like this:

var date1 = new Date(2016,9,20,12,0,0); // October 20, 2016 12:00:00
var date2 = new Date(2016,9,20,12,1,0); // October 20, 2016 12:01:00

// Comparison including time.
moment(date2).isAfter(date1); // => true

// Comparison excluding time.
moment(date2).isAfter(date1, 'day'); // => false

The second parameter you pass into is the precision to do the comparison and can be any of , , , , , or .isAfteryearmonthweekdayhourminutesecond

评论

6赞 2c2c 8/11/2017
got a 100x speedup moving from a momentjs solution to a Date() + sethours solution. careful friends :-)
0赞 Joshua Pinter 8/11/2017
@2c2c Interesting. Definitely good to know the performance impact. How many operations did you use for your benchmark?
1赞 2c2c 8/12/2017
i was at around 1mil comparisons. didnt setup an actual benchmark
3赞 bbsimonbb 9/22/2020
Moment is in maintenance mode and should no longer be used if you're not using it already. There are alternatives, and the date object is better than it used to be.
26赞 Rahi 2/3/2017 #12

Simply compare using .toDateString like below:

new Date().toDateString();

This will return you date part only and not time or timezone, like this:

"Fri Feb 03 2017"

Hence both date can be compared in this format likewise without time part of it.

评论

4赞 Grid Trekkor 2/28/2017
That's a good start, but it won't allow for comparisons because it's now a string. To compare, change it back to a Date object by doing new Date(new Date().toDateString());
2赞 C.M. 5/13/2020
@GridTrekkor True, but if you just want a quick equality test this is the most simple solution.
0赞 chitgoks 9/5/2023
As much as possible I do not want to use another lib. This is perfect!
0赞 R K Sharma 1/5/2018 #13

I know this question have been already answered and this may not be the best way, but in my scenario its working perfectly, so I thought it may help someone like me.

if you have as date string

String dateString="2018-01-01T18:19:12.543";

and you just want to compare the date part with another Date object in JS,

var anotherDate=new Date(); //some date

then you have to the to object by using convertstringDatenew Date("2018-01-01T18:19:12.543");

and here is the trick :-

var valueDate =new Date(new Date(dateString).toDateString());

            return valueDate.valueOf() == anotherDate.valueOf(); //here is the final result

I have used of of JS, which returns the Date string only. toDateString()Date object

Note: Don't forget to use the function while comparing the dates..valueOf()

more info about is here reference.valeOf()

Happy codding.

8赞 robocat 3/8/2018 #14

An efficient and correct way to compare dates is:

Math.floor(date1.getTime() / 86400000) > Math.floor(date2.getTime() / 86400000);

It ignores the time part, it works for different timezones, and you can compare for equality too. 86400000 is the number of milliseconds in a day (). === 24*60*60*1000

Beware that the equality operator should never be used for comparing Date objects because it fails when you would expect an equality test to work because it is comparing two Date objects (and does not compare the two dates) e.g.:==

> date1;
outputs: Thu Mar 08 2018 00:00:00 GMT+1300

> date2;
outputs: Thu Mar 08 2018 00:00:00 GMT+1300

> date1 == date2;
outputs: false

> Math.floor(date1.getTime() / 86400000) == Math.floor(date2.getTime() / 86400000);
outputs: true

Notes: If you are comparing Date objects that have the time part set to zero, then you could use but it is hardly worth the optimisation. You can use , , , or when comparing Date objects directly because these operators first convert the Date object by calling before the operator does the comparison.date1.getTime() == date2.getTime()<><=>=.valueOf()

评论

0赞 Omu 5/10/2023
won't work; example change hour to and you get a different integer (floor) result.new Date (2017, 10, 2, 1).getTime()/864000002
0赞 robocat 5/11/2023
@omu Perhaps you need to learn to test better. It does work, although rather ugly. Perhaps needs rounding to nearest second if times are to nanosecond, but other answers have same fault. I think some of the other answers are better for clarity and maintainance.
0赞 Omu 5/14/2023
Math.round won't help, try it in chrome dev tools console, , using you'll get different results at hour 12 and 13new Date (2017, 10, 2, 1).getTime()/86400000Math.round
0赞 robocat 5/15/2023
@omu You are starting the bleeding obvious and I didn't say Math.round() would work: how you code it depends upon your epsilon. And saying Math.floor() doesn't work at 12 and 13 is saying that Math.floor() does work!!! So far you haven't actually included the Math.floor() code you believe doesn't work. I tested using Math.floor(), and had no problem. Please include a fully worked example using Math.floor() and give the outputs you get.
0赞 robocat 5/15/2023
@omu Link to a jsbin.com (or equivalent) that demonstrates your problem. I don't work in JavaScript any more, and I am not really that keen to help you solve your problem :-)
0赞 Hiran Walawage 6/28/2018 #15

This will help. I managed to get it like this.

var currentDate = new Date(new Date().getFullYear(), new Date().getMonth() , new Date().getDate())
1赞 Chandrashekhar Kase 2/23/2019 #16

Comparing with will be a solution. Sample:setHours()

var d1 = new Date();
var d2 = new Date("2019-2-23");
if(d1.setHours(0,0,0,0) == d2.setHours(0,0,0,0)){
    console.log(true)
}else{
    console.log(false)
}
1赞 Pritesh Singh 2/26/2019 #17
var fromdate = new Date(MM/DD/YYYY);
var todate = new Date(MM/DD/YYYY);
if (fromdate > todate){
    console.log('False');
}else{
    console.log('True');
}

if your date formate is different then use moment.js library to convert the format of your date and then use above code for compare two date

Example :

If your Date is in "DD/MM/YYYY" and wants to convert it into "MM/DD/YYYY" then see the below code example

var newfromdate = new Date(moment(fromdate, "DD/MM/YYYY").format("MM/DD/YYYY"));
console.log(newfromdate);
var newtodate = new Date(moment(todate, "DD/MM/YYYY").format("MM/DD/YYYY"));
console.log(newtodate);

17赞 brittohalloran 4/21/2019 #18

If you are truly comparing date only with no time component, another solution that may feel wrong but works and avoids all time and timezone headaches is to compare the ISO string date directly using string comparison:Date()

> "2019-04-22" <= "2019-04-23"
true
> "2019-04-22" <= "2019-04-22"
true
> "2019-04-22" <= "2019-04-21"
false
> "2019-04-22" === "2019-04-22"
true

您可以使用以下命令获取当前日期(UTC 日期,不一定是用户的本地日期):

> new Date().toISOString().split("T")[0]
"2019-04-22"

我支持它的论点是程序员的简单性 - 与尝试正确处理日期时间和偏移量相比,你不太可能搞砸这一点,这可能是以速度为代价的(我没有比较性能)

评论

1赞 stomy 8/27/2019
你也可以做.new Date().toISOString().substr(0, 10) // "2019-04-22"
0赞 stomy 8/27/2019
对于本地日期(非 UTC 日期),您可以使用 。但是比较文本将不准确(喜欢是错误的)。因此,您需要将其转换为与另一个日期进行准确比较。new Date().toLocaleDateString() // "8/26/2019""8/26/2019" >= "9/29/2001"new Date(new Date().toLocaleDateString())
3赞 NICHOLAS WOJNAR 5/1/2019 #19

这个JS将在设定的日期之后更改内容,这是同样的事情,但在w3schools上

date1 = new Date()
date2 = new Date(2019,5,2) //the date you are comparing

date1.setHours(0,0,0,0)

var stockcnt = document.getElementById('demo').innerHTML;
if (date1 > date2){
document.getElementById('demo').innerHTML="yes"; //change if date is > set date (date2)
}else{
document.getElementById('demo').innerHTML="hello"; //change if date is < set date (date2)
}
<p id="demo">hello</p> <!--What will be changed-->
<!--if you check back in tomorrow, it will say yes instead of hello... or you could change the date... or change > to <-->

17赞 Mike Feltman 5/17/2019 #20

只需在两个日期都使用 toDateString() 即可。toDateString 不包括时间,因此对于同一日期的 2 次,值将相等,如下所示。

var d1 = new Date(2019,01,01,1,20)
var d2 = new Date(2019,01,01,2,20)
console.log(d1==d2) // false
console.log(d1.toDateString() == d2.toDateString()) // true

显然,在其他地方表达的关于这个问题的一些时区问题是有效的,但在许多情况下,这些问题是无关紧要的。

评论

4赞 Mr.K 12/10/2019
就我而言,这个解决方案比选定的答案和投票次数更多的答案要好得多。在标题中明确指出“无需比较时间”,此解决方案正是这样做的,从而节省了设置时间来操作比较的需要。要回答“事件 X 和事件 Y 是否在同一天发生”,只比较日期比回答“事件 X 日期的 00:00:00.00 AM 与事件 Y 日期的 00:00:00.00 是否完全相同”更有意义,这是许多其他回答者正在做的事情
0赞 user2849789 7/19/2019 #21

您可以使用 fp_incr(0)。它将时区部分设置为午夜并返回日期对象。

2赞 Yogesh Devgun 12/9/2019 #22

这对我有用:

 export default (chosenDate) => {
  const now = new Date();
  const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
  const splitChosenDate = chosenDate.split('/');

  today.setHours(0, 0, 0, 0);
  const fromDate = today.getTime();
  const toDate = new Date(splitChosenDate[2], splitChosenDate[1] - 1, splitChosenDate[0]).getTime();

  return toDate < fromDate;
};

在接受的答案中,存在时区问题,而其他时间不是 00:00:00

0赞 SridharKritha 1/21/2022 #23

比较日期和时间:

var t1 = new Date(); // say, in ISO String =  '2022-01-21T12:30:15.422Z'
var t2 = new Date(); // say, in ISO String =  '2022-01-21T12:30:15.328Z'
var t3 = t1;

按毫秒级别比较 2 个日期对象:

console.log(t1 === t2); // false - Bcos there is some milliseconds difference
console.log(t1 === t3); // true - Both dates have milliseconds level same values

仅按日期比较 2 个日期对象(忽略任何时差):

console.log(t1.toISOString().split('T')[0] === t2.toISOString().split('T')[0]); 
                                        // true; '2022-01-21' === '2022-01-21'

仅按时间(毫秒)比较 2 个日期对象(忽略任何日期差异):

console.log(t1.toISOString().split('T')[1] === t3.toISOString().split('T')[1]); 
                                      // true; '12:30:15.422Z' === '12:30:15.422Z'

以上 2 种方法使用方法,因此您无需担心各国之间的时区差异toISOString()

0赞 rpf3 5/17/2022 #24

我最终使用的一个选项是使用 Moment.js 的功能。通过调用类似的东西,您可以比较整数天数的差异。diffstart.diff(end, 'days')

0赞 akm elias 7/25/2022 #25

对我有用: 我需要将日期与本地 dateRange 进行比较

let dateToCompare = new Date().toLocaleDateString().split("T")[0])
let compareTime = new Date(dateToCompare).getTime()

let startDate = new Date().toLocaleDateString().split("T")[0])
let startTime = new Date(startDate).getTime()

let endDate = new Date().toLocaleDateString().split("T")[0])
let endTime = new Date(endDate).getTime()

return compareTime >= startTime && compareTime <= endTime
0赞 Jannunen 10/10/2022 #26

像往常一样。太少,太晚了。

现在不鼓励使用 momentjs(他们的话,不是我的话),而 dayjs 是首选。

可以使用 dayjs 的 isSame。

https://day.js.org/docs/en/query/is-same

dayjs().isSame('2011-01-01', 'date')

您还可以使用许多其他单位进行比较: https://day.js.org/docs/en/manipulate/start-of#list-of-all-available-units

2赞 Zeshan 12/5/2022 #27

使用 javascript,您可以将现有日期对象的时间值设置为零,然后解析回 .解析回 后,两者的时间值均为 0,可以做进一步的比较。DateDate

      let firstDate = new Date(mydate1).setHours(0, 0, 0, 0);
      let secondDate = new Date(mydate2).setHours(0, 0, 0, 0);

      if (firstDate == secondDate)
      {
        console.log('both dates are on same day');
      }
      else
      {
        console.log(`both dates are not on same day`);
      }
0赞 thisismydesign 2/11/2023 #28

使用知道自己在做什么的库

https://day.js.org/docs/en/query/is-same-or-before

dayjs().isSameOrBefore(date, 'day')