0

I'm in trouble with dates.

Note: I'm into Europe/Rome timezone.

I have the following date

....
new Date("2019-04-03T00:00:00+02:00")

 it returns:  Wed Apr 03 2019 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
....

If I change timezone (GMT -4) on my PC it returns, for example,

....
new Date("2019-04-03T00:00:00+02:00")
 it returns:  Tue Apr 02 2019 18:00:00 GMT-0400 (GMT-04:00)
....

What I want to do is to the date (2019-04-03T00:00:00+02:00) to a user's calendar without consider users timezone (even if it is in Canada I want to display on the user calendar Europe/Rome timezone.

Do you have any suggestions?

Thank you

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • The date shown in the console, "Tue Apr 02 2019 18:00:00 GMT-0400 (GMT-04:00)" is the exact same point in time as the date you passed in, "2019-04-03T00:00:00+02:00". If you want to display the date and time in the user's time zone, format the date in that time zone. – Heretic Monkey Apr 04 '19 at 21:15
  • @ Heretic Monkey It is not a duplicate. – Damien Apr 04 '19 at 21:38

2 Answers2

0

You can use moment.js and moment-timezone.js to achieve the result.

let ny = moment().tz("Europe/Rome").format();

console.log(ny);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.js"></script>
Damien
  • 1,582
  • 1
  • 13
  • 24
0

Generally, you want to use dates only as UTC, and convert them to whatever TZ you want when you display them. UTC in js

Moment is pretty good for these things,

const moment = require("moment-timezone")
const now = moment.utc()
const nowButItsInYourTimezone = now.tz("Europe/Rome")
console.log(now.format()) // utc date
console.log(nowButItsInYourTimezone.format()) // gmt + 2, of the same date