0

I'm trying to compare given date with current date, It is working fine for Indian timezone but other time zones getting one day off. I have went through similar type of issues and there suggestions are like convert UTC string. After converting UTC string getting correct date while console but when comparing not get correct result. Here my given date in the format like 'YYYY-MM-DD'. The initial comparison was like below, and this will work fine for Indian timezone.

              const datestring = '2019-05-06';
              const q = new Date();
              const m = q.getMonth();
              const d = q.getDate();
              const y = q.getFullYear();
              const currentDate = new Date(y, m, d);
              const givenDate = new Date(datestring);
              if (currentDate <= givenDate) {
                return null;
              } else {
                return {
                  'currentDateChecker': true
                };
              }
            }

The above one will work fine for Indian time zone and for some other time zone apart from Indian time zone, it is giving one day less.

But after converting to UTC like:

const givenDate = new Date(datestring).toUTCString();

Now this will give correct date but for comparing purpose I have converted both current date also to UTC string, by that time result is not coming as expected. I know there are number of articles existed related to this but not getting proper way so only posting this question.

Zach Smith
  • 8,458
  • 13
  • 59
  • 133
Roy
  • 880
  • 7
  • 21
  • 39
  • 2
    The root cause would be that you're giving ambiguous date string to initialize a `Date` object, which results in it using the local timezone set by system it runs on. You'd want to receive the date string, as well as its timezone information (preferably an ISO string) to initialize the `Date` object to compare against. – woozyking May 06 '19 at 13:55
  • possible duplicate - https://stackoverflow.com/questions/7556591/is-the-javascript-date-object-always-one-day-off – Naga Sai A May 06 '19 at 13:57

2 Answers2

2

The difficulty is that new Date() will create a date in YOUR timezone. For me, since I'm SAST (+2 hours), if I say new Date(2019, 4, 6) I will get the UTC datetime 5 May 22:00 2019. This makes it difficult to compare dates, since someone in India who wanted to compare "6 May 2019" will actually get a UTC date 6 May 2019, and that won't equal 5 May 2019.

Instead use Date.UTC(year, month, day) - an epoch time is number of milliseconds since 2970, Jan 1st in UTC.

const today = new Date()
const todayYear = today.getFullYear()
const todayMonth = today.getMonth()
const todayDay = today.getDate()
const todayUtc = Date.UTC(todayYear, todayMonth, todayDay)

const dateString = 'yyyy-mm-dd'
const dateArr = dateString.split('-')
const dateUtc = Date.UTC(dateArr[0], dateArr[1], dateArr[2])

if (todayUtc === dateUtc) {
  // ...
}
Zach Smith
  • 8,458
  • 13
  • 59
  • 133
  • Thanks for the solution.But here for getting todayMonth we need to add plus 1 right..while defining datestring manually..otherwise it will take month from 0 and our date string value we wont give month count start with zero correct. – Roy May 06 '19 at 18:17
  • 1
    Yes - months are 0 - 11 – Zach Smith May 06 '19 at 18:32
0

You can use getTime to do the comparaison:

// ...
const currentDate = new Date(y, m, d).getTime();
const givenDate = new Date(datestring).getTime();
if (currentDate <= givenDate) {
  return null;
} else {
  return {
    'currentDateChecker': true
  };
}
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
  • This won't work if you want to compare a 'yyyy-mm-dd' date from two different timezones. if I say `new Date(y, m, d)` I will actually get 22:00 of the previous day unless my environment is set to UTC – Zach Smith May 06 '19 at 14:05
  • Yes, but It's just a demonstration on how to compare 2 date. Format is another way to handle. So why the downvote? – Radonirina Maminiaina May 06 '19 at 14:10
  • 2
    Because it won't work across timezones, which is probably relevant. (In his question he mentioned that he was having timezone problems: "It is working fine for Indian timezone but other time zones getting one day off") – Zach Smith May 06 '19 at 15:49