33

I have a date object in JavaScript and I want to figure out if that date is today. What is the fastest way of doing this?

My concern was around comparing date object as one might have a different time than another but any time on today's date should return true.

Shashank Agrawal
  • 23,503
  • 10
  • 80
  • 110
leora
  • 177,207
  • 343
  • 852
  • 1,351

5 Answers5

85

You could use toDateString:

var d = new Date()
var bool = (d.toDateString() === otherDate.toDateString());
Shashank Agrawal
  • 23,503
  • 10
  • 80
  • 110
Joseph Marikle
  • 72,900
  • 16
  • 109
  • 126
  • Thank you @AKX for fixing that. Don't know what came over me :P – Joseph Marikle Dec 06 '11 at 00:50
  • 1
    @SearchForKnowledge format is of less concern. The original question presupposes that your variable is in a javascript date object format to begin with. `01192015` is technically an invalid date format (`new Date('01192015')` in console returns "Invalid Date") at least as far as javascript is concerned. However, using the standard `01-19-2015` returns a valid date object, which you can use to compare with todays date using `toDateString()`. – Joseph Marikle Jan 23 '15 at 23:12
12

The answers based on toDateString() will work I think, but I personally would avoid them since they basically ask the wrong question.

Here is a simple implementation:

function areSameDate(d1, d2) {
    return d1.getFullYear() == d2.getFullYear()
        && d1.getMonth() == d2.getMonth()
        && d1.getDate() == d2.getDate();
}

MDN has a decent overview of the JS Date object API if this isn't quite what you need.

rgahan
  • 523
  • 6
  • 14
recf
  • 683
  • 4
  • 8
  • 5
    Please do not link to W3Schools. They have [misleading and often incorrect content](http://w3fools.com/). Instead, use a more well-reputed source like [Mozilla's excellent developer network](https://developer.mozilla.org/en-US/) – Andrew Whitaker Dec 06 '11 at 01:08
  • I was not aware of the issues with W3Schools. Updated the link to point to the relevant MDN doc page. – recf Dec 06 '11 at 01:17
  • +1 as I think this is a more robust solution than toDateString. – RobG Dec 06 '11 at 01:39
  • I also like this solution better for my point you are also using the clock to check, – mortenstarck Mar 08 '17 at 13:01
  • I've found in my experience this to be the most surefire method of basic date checking. using toDateString() is not reliable, as any minor diff in format can cause a false check. –  dandmcd Feb 28 '21 at 05:41
5
var someDate = new Date("6 Dec 2011").toDateString();
var today = new Date().toDateString();
var datesAreSame = (today === someDate);
Chris Fulstow
  • 39,861
  • 10
  • 85
  • 109
3

If both are Date() objects, you can use this to 'format' the date in a way that it will only compare on the year/month/day: if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0));

Nearly identical question: How to check if input date is equal to today's date?

Community
  • 1
  • 1
AvatarKava
  • 15,000
  • 1
  • 26
  • 33
1

I prefer to use moment lib

moment('dd/mm/yyyy').isSame(Date.now(), 'day');
Aleksandr Golovatyi
  • 933
  • 1
  • 10
  • 16