1

I'm getting crazy with showing a date correctly to the client:

From the API I receive data.expiry_date and its value is: 2017-09-06T23:59:59Z

The client to show that data looks like this:

var date = new Date(data.expiry_date);
$('#expiry_date').val(`${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`);

The result is a day before than expected: it should be 06/09/2017 but it show 07/09/2017.

Basically the date value from the action var date = new Date(data.expiry_date); is: Thu Sep 07 2017 01:59:59 GMT+0200 (CEST).

How can I get rid of it?

Ayeye Brazo
  • 3,316
  • 7
  • 34
  • 67
  • Sorry, a typo while copy / pasting logs. I corrected my question. Thanks for letting me know! – Ayeye Brazo Sep 14 '17 at 10:53
  • If you just want to display the date as 07/09/2017, just reformat the string. There's no need to parse it to a Date then format it, that's just way more work than is necessary. – RobG Sep 15 '17 at 01:33

1 Answers1

3

You're not in the same time zone. 2017-09-06T23:59:59Z is UTC which is same as Thu Sep 07 2017 01:59:59 GMT+0200 (CEST)

Brahma Dev
  • 1,955
  • 1
  • 12
  • 18
  • And how can I show the correct date to all users? All other expiry dates are ok, this one has one day less... it should display 07/09/2017 independently by the time zone... – Ayeye Brazo Sep 14 '17 at 11:00
  • @AyeyeBrazo It does not have one day less/more, It has two hours less/more because you are running 2 hours ahead of UTC. It is the correct date/time. – Brahma Dev Sep 14 '17 at 11:02