0

I am getting wrong result while comparing two dates using Javascript. I am explaining my code below.

var user_date='01-04-2019';
var todayDate = new Date();
var todayMonth = todayDate.getMonth() + 1;
var todayDay = todayDate.getDate();
var todayYear = todayDate.getFullYear();
if (todayDay < 10) {
    todayDay = '0' + todayDay;
}
if (todayMonth < 10) {
    todayMonth = '0' + todayMonth;
}
var todayDateText = todayDay + "-" + todayMonth + "-" + todayYear;
var inputToDate = Date.parse(user_date);
var todayToDate = Date.parse(todayDateText);
console.log(todayDateText);
//console.log(mydate);
if (inputToDate > todayToDate) {
    alert("the input is later than today");
}else{
    alert("the input is earlier than today");
}

Here I am getting the else part alert message where user input date is later of today's date.

Blue
  • 22,608
  • 7
  • 62
  • 92
subhra
  • 173
  • 5
  • if(new Date('01-04-2019').getTime() > new Date().getTime()) { //Date greater than today's date } else { //Date lesser than today's date } – Deepak A Mar 30 '19 at 06:35
  • ref https://stackoverflow.com/questions/8215556/how-to-check-if-input-date-is-equal-to-todays-date – Deepak A Mar 30 '19 at 06:36

3 Answers3

1

The problem is with Date.parse() which does not parse DD-MM-YYYY correctly. Here is an working example with MM-DD-YYYY (Note : YYYY-MM-DD recommended)

var user_date = '03-01-2019'; // MM-DD-YYYY
var todayDate = new Date();
var todayMonth = todayDate.getMonth() + 1;
var todayDay = todayDate.getDate();
var todayYear = todayDate.getFullYear();
if (todayDay < 10) {
  todayDay = '0' + todayDay;
}
if (todayMonth < 10) {
  todayMonth = '0' + todayMonth;
}
var todayDateText = todayMonth + "-" + todayDay + "-" + todayYear;
var inputToDate = Date.parse(user_date);
var todayToDate = Date.parse(todayDateText);
console.log(inputToDate, todayToDate);
console.log(user_date, todayDateText);
if (inputToDate > todayToDate) {
  alert("the input is later than today");
} else {
  alert("the input is earlier than today");
}

To convert DD-MM-YYYY to MM-DD-YYYY, use

var user_date ='01-03-2019'; // DD-MM-YYYY
var datePieces = user_date.split("-"); 
console.log([datePieces[1] , datePieces[0] , datePieces[2]].join("-")); // 03-01-2019
User863
  • 19,346
  • 2
  • 17
  • 41
  • 1
    [No guarantees that `MM-DD-YYYY` will work either.](https://www.ecma-international.org/ecma-262/6.0/#sec-date.parse) – AuxTaco Mar 30 '19 at 07:37
  • @AuxTaco yeah the recommended string is `YYYY-MM-DD`.i will edit the answer. – User863 Mar 30 '19 at 07:39
0

Btw, in the future give momentJS a try. It's a third-party library that many devs use to deal with time and dates because we all know that this is a pain in vanilla Javascript.

https://momentjs.com

Ernie
  • 186
  • 1
  • 1
  • 10
0

Don't use Date.parse. It's not guaranteed to understand a date string of the format dd-mm-yyyy:

console.log(Date.parse('01-04-2019'))

Instead, use the 2+-argument Date constructor and compare dates directly:

var userDate = new Date(2019, 3 /* months are 0-indexed */, 1);
var todayDate = new Date();

// drop the time part of todayDate
todayDate.setHours(0, 0, 0, 0);

if (userDate > todayDate) {
    alert("the input is later than today");
} else {
    alert("the input is no later than today");
}
AuxTaco
  • 4,883
  • 1
  • 12
  • 27