0

I am not so into JavaScript and I have to check if 2 date have the same day\month\year (not the time, so only if the day is the same). I have to use only old plain JavaScript

I have 2 Date variable and I was trying to do in this way:

if(previousDate.getDate() ===  dateCurrentForecast.getDate()) {
    console.log("SAME DATE");
}

Where:

  • previousDate = Sun Nov 05 2017 06:00:00 GMT+0100 (ora solare Europa occidentale)

  • dateCurrentForecast = Sun Nov 05 2017 12:00:00 GMT+0100 (ora solare Europa occidentale)

I have to find a way to check that these 2 date are in the same day (Sun Nov 05 2017).

Using the previous snipet it seems to work because both previousDate.getDate() and dateCurrentForecast.getDate() returns the value 5, but what exactly means this returned value?

What is the correct way to do this kind of check?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • You should take a look at date methods in JavaScript here https://www.w3schools.com/js/js_date_methods.asp – Alvis Nov 07 '17 at 13:47
  • Well... you can to read this post: [https://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript](https://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript) I hope it helps you – Mario Junior Torres Perez Nov 07 '17 at 13:48
  • You can use momentjs to get this as well. [Check](https://stackoverflow.com/a/28346741/3551786) – Durga Nov 07 '17 at 13:58

5 Answers5

2

You can remove timestamp part of date and then compare the value:

Note: Remember to create new objects else you will mutate original objects.

function isSameDay(d1, d2) {
  var _d1 = new Date(+d1);
  var _d2 = new Date(+d2);
  
  _d1.setHours(0,0,0,0)
  _d2.setHours(0,0,0,0)
  
  return +_d1 === +_d2;
}

var d1 = new Date('Sun Nov 05 2017 06:00:00 GMT+0100 (ora solare Europa occidentale)');
var d2 = new Date('Sun Nov 05 2017 12:00:00 GMT+0100 (ora solare Europa occidentale)')
console.log(isSameDay(d1, d2))

or you can check for values manually:

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

var d1 = new Date('Sun Nov 05 2017 06:00:00 GMT+0100 (ora solare Europa occidentale)');
var d2 = new Date('Sun Nov 05 2017 12:00:00 GMT+0100 (ora solare Europa occidentale)')
console.log(isSameDay(d1, d2))
Rajesh
  • 24,354
  • 5
  • 48
  • 79
1

Your actual code will work even if the two dates have different months or years when they have the same day.

You need to check upon year, month and day:

function areThe2DatesEqualByDay(d1, d2) {
  if (d1.getYear() === d2.getYear()) {
    if (d1.getMonth() === d2.getMonth()) {
      if (d1.getDate() === d2.getDate())
        return true;
    }
  }
  return false;
}

Demo:

var previousDate = "Sun Nov 05 2017 06:00:00 GMT+0100";
var dateCurrentForecast = "Sun Nov 05 2017 12:00:00 GMT+0100";

var diffDate = "Sun Mar 05 2017 12:00:00 GMT+0100";

function areThe2DatesEqualByDay(d1, d2) {
  if (d1.getYear() === d2.getYear()) {
    if (d1.getMonth() === d2.getMonth()) {
      if (d1.getDate() === d2.getDate())
        return true;
    }
  }
  return false;
}

console.log(areThe2DatesEqualByDay(new Date(), new Date()));

console.log(areThe2DatesEqualByDay(new Date(previousDate), new Date(dateCurrentForecast)));

console.log(areThe2DatesEqualByDay(new Date(previousDate), new Date(diffDate)));
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
1

getDate returns the day of the month. So you are now only checking if the day of the month is the samen. With getMonth and getYear you could check the month and the year of both dates.

if (previousDate.getDate() ===  dateCurrentForecast.getDate() && previousDate.getMonth() ===  dateCurrentForecast.getMonth() && previousDate.getYear() ===  dateCurrentForecast.getYear()) { console.log('the same'); }
Jasper Seinhorst
  • 1,056
  • 6
  • 19
1

The getDate function returns the day of the month, that the Date object represents. So to check if two Date objects represents the say day, while ignoring the time of day, you need to compare the day of month, the month, and the year.

var d1 = new Date("Sun Nov 05 2017 06:00:00 GMT+0100");
var d2 = new Date("Sun Nov 05 2017 12:00:00 GMT+0100");
var sameDate = d1.getDate() === d2.getDate() && d1.getMonth() === d2.getMonth() && d1.getYear() == d2.getYear();
console.log("Are they the same date?", sameDate);
1

JavaScript Date object methods are sometimes unobvious because getDate method returns day of the month and not a date string as it might be expected from method name.

You can accomplish your task by creating copy of your date objects and comparing them. It may be good idea to use UTC versions of methods to avoid cross-timezone differences:

function isSameDay(d1, d2) {
  var date1 = new Date(Date.UTC(d1.getYear(), d1.getMonth(), d1.getDay()));
  var date2 = new Date(Date.UTC(d2.getYear(), d2.getMonth(), d2.getDay()));
  return date1.getTime() ===  date2.getTime();
}

console.log(isSameDay(
  new Date('Sun Nov 05 2017 06:00:00 GMT+0100'),
  new Date('Sun Nov 05 2017 12:00:00 GMT+0100')
));

console.log(isSameDay(
  new Date('Sun Nov 06 2017 06:00:00 GMT+0100'),
  new Date('Sun Nov 05 2017 12:00:00 GMT+0100')
));
Flying
  • 4,422
  • 2
  • 17
  • 25