103

I have a form input with an id of 'date_trans'. The format for that date input (which is validated server side) can be any of:

  • dd/mm/yyyy
  • dd-mm-yyyy
  • yyyy-mm-dd
  • yyyy/mm/dd

However, before posting the form, I'd like to check if the date_trans field has a date that is equal to today's date. Its ok if the date taken is the client's date (i.e. it uses js), since I run a double check on the server as well.

I'm totally lost on how to do the date comparrison in jQuery or just plain old javascript. If it helps, I am using the jquery datepicker

JonoB
  • 5,801
  • 17
  • 55
  • 77
  • BTW, those are some silly date formats you're accepting. If you have any control over the decision to accept them, I'd suggest rethinking the usefulness/necessity of them all. – Phrogz Nov 21 '11 at 17:13
  • What's silly about them? We operate in multiple countries, and they are accepted formats. I suppose that you think that everyone works on the (ridiculous) US format? – JonoB Nov 21 '11 at 20:52
  • 1
    Goodness, no! The US Format is appalling, but it's not present in your list and yet would be easily confused by your first case. If you are sure that all four are necessary, by all means use them. To me it seems like you have chosen to be both arbitrarily permissive and arbitrarily restrictive at the same time. *shrug* – Phrogz Nov 21 '11 at 21:04
  • The US Format works just fine and has worked for many MANY years. – metal_jacke1 Oct 21 '20 at 17:56

11 Answers11

224

A simple date comparison in pure JS should be sufficient:

// Create date from input value
var inputDate = new Date("11/21/2011");

// Get today's date
var todaysDate = new Date();

// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
    // Date equals today's date
}

Here's a working JSFiddle.

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
James Hill
  • 60,353
  • 20
  • 145
  • 161
122

for completeness, taken from this solution:

You could use toDateString:

var today = new Date();
var isToday = (today.toDateString() == otherDate.toDateString());

no library dependencies, and looking cleaner than the 'setHours()' approach shown in a previous answer, imho

Community
  • 1
  • 1
schellmax
  • 5,678
  • 4
  • 37
  • 48
  • 1
    This is tricky. In the case of a angular watch I have newDates[i] =1476252000000 and oldDates[i] === 1476252000000. However, they are not equal. Date.parse(newDates[1]) === Date.parse(oldDates[1]) returns false. – Winnemucca Oct 07 '16 at 23:21
  • @Winnemucca Returns `true` for me for the given values. You don't have to use `Date.parse` for equality check – Slartibartfast Mar 16 '17 at 05:13
  • 3
    @Winnemucca, it's not always better to use ===. – PRMan Jun 28 '18 at 23:05
  • when you are dealing with a huge number of comparisons, the integer comparison outperforms against strings. [This source](https://stackoverflow.com/questions/4904179/why-is-integer-comparsion-faster-then-string-comparion) is for c++, but there is no difference since it's at machine level – Navid Jul 28 '19 at 06:06
  • While this is indeed clean, as the previous also comment mentions the performance is really slow, and for high-performing tasks, like charting libraries with high volume of tickers, this is not valuable. Check also the result of a simple benchmark here: https://jsbench.me/mal2yl062a/1 – konsalex May 09 '22 at 10:30
24

Try using moment.js

moment('dd/mm/yyyy').isSame(Date.now(), 'day');

You can replace 'day' string with 'year, month, minute' if you want.

Cengkaruk
  • 475
  • 5
  • 5
12
function sameDay( d1, d2 ){
  return d1.getUTCFullYear() == d2.getUTCFullYear() &&
         d1.getUTCMonth() == d2.getUTCMonth() &&
         d1.getUTCDate() == d2.getUTCDate();
}

if (sameDay( new Date(userString), new Date)){
  // ...
}

Using the UTC* methods ensures that two equivalent days in different timezones matching the same global day are the same. (Not necessary if you're parsing both dates directly, but a good thing to think about.)

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • You should mention, that this is actually the month index and not the month number itself, thus one needs to add 1 – Julian F. Weinert Sep 05 '15 at 10:57
  • Wouldnt it be faster in most cases to first check for equal days, then month and then year? – Jonathan Sep 24 '15 at 14:12
  • 1
    @xcy7e웃 Depends on the input data, but possibly. However, if you can create a test case where this actually makes any measurable difference at all, I'll be very impressed. – Phrogz Sep 24 '15 at 14:25
  • Yes you are right there will be not much measurable difference - but I'm talking about philosophy ;) – Jonathan Sep 24 '15 at 14:48
5

Just use the following code in your javaScript:

if(new Date(hireDate).getTime() > new Date().getTime())
{
//Date greater than today's date 
}

Change the condition according to your requirement.Here is one link for comparision compare in java script

Ravi Kant
  • 4,785
  • 2
  • 24
  • 23
1

The following solution compares the timestamp integer divided by the values of hours, minutes, seconds, millis.

var reducedToDay = function(date){return ~~(date.getTime()/(1000*60*60*24));};
return reducedToDay(date1) == reducedToDay(date2)

The tilde truncs the division result (see this article about integer division)

Community
  • 1
  • 1
L-Ray
  • 1,637
  • 1
  • 16
  • 29
0

Try this

// method to check date is less than today date
  isLessDate(schedule_date : any){
    var _schedule_date = new Date(schedule_date);
    var date = new Date();
    var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
    var _today_date = new Date(''+transformDate);
    if(_schedule_date < _today_date){
      return 'small'
    }
    else if(_schedule_date > _today_date){
      return 'big'
    }
    else {
      return 'same'
    }
  }
shehan96
  • 328
  • 1
  • 3
  • 20
-1

The Best way and recommended way of comparing date in typescript is:

var today = new Date().getTime();
var reqDateVar = new Date(somedate).getTime();

if(today === reqDateVar){
 // NOW
} else {
 // Some other time
}
Dineshgaru
  • 181
  • 1
  • 3
  • 16
-1

Date.js is a handy library for manipulating and formatting dates. It can help in this situation.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
-3
TodayDate = new Date();
if (TodayDate > AnotherDate) {} else{}

< = also works, Although with =, it might have to match the milliseconds.

Ronk
  • 215
  • 2
  • 11
-3

There is a simpler solution

if (inputDate.getDate() === todayDate.getDate()) {
   // do stuff
}

like that you don't loose the time attached to inputDate if any

Adwich
  • 13
  • 1
  • 4
    This solution has many issues `.getDate()` only returns the numerical day of the month. So if you use this it would return true if one date was the 10th of August 1962 and the other date was the 10th of October 2025. – Daniel Tonon Aug 30 '18 at 06:18