0

I am calling an Api that has the dates in the following format: 2020-09-08T00:00:00Z. I want to get the current date of the user and compare it against the API date to see how much time is left. The problem is that users could be in UK or IE and the second problem how can I be sure that there is not a daylight saving time issue here. I would also like to display the dates to the user.

const userDate = new Date() //current date
const apiDate = new Date('2020-09-08T00:00:00Z') //api date
if (userDate.getTime() <= apiDate.getTime())
    //do something
retroman
  • 175
  • 1
  • 11
  • just to add - user date is from the browser – retroman Oct 12 '20 at 18:15
  • "*users could be in UK or IE*" well, `new Date()` will give you the local date and `getTime()` gives you a Unix timestamp which is always in UTC. "*the second problem how can I be sure that there is not a daylight saving time issue here*" UTC doesn't have daylight savings. – VLAZ Oct 12 '20 at 18:22
  • Users being on Internet Exploder might be a problem. Users in the UK are not. `new Date()` gives you the current timestamp, not timezones attached. – Bergi Oct 12 '20 at 18:41
  • 1
    Please limit posts to one question. `new Date('2020-09-08T00:00:00Z') - Date.now()` will give the difference in milliseconds. Do you trust the client's clock? Daylight saving is irrelevant. To display use one of the various [*toString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) methods. All these questions have been answered many times already. – RobG Oct 13 '20 at 09:31
  • @Bergi—I think the OP means Ireland, not Internet Explorer. – RobG Oct 13 '20 at 09:34
  • Thanks yes I mean Ireland (IE). My confusion is regarding the 'z' at the end of the date. I think this means zulu time or UTC zero. I would like to create a date from this and compare it with the current date of the browser ( new Date()). I'm worried that the new date will be in local time and the api in UTC, so when i compare it will not be correct. So the question is how to compare a 'z' date with the current date in UTC (z) – retroman Oct 13 '20 at 10:15
  • @retroman—ECMAScript dates are inherently UTC, so `new Date('2020-09-08T00:00:00Z') < Date.now()` is sufficient, see [*Compare two dates with JavaScript*](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript). – RobG Oct 14 '20 at 02:45

0 Answers0