0

So I have the below code, which does a bunch of conversions between dates/strings/moments, and it all seems to be converting properly until the line that calls .toLocaleString(), not sure why but it is converting 2022-05-04T17:00:00.000Z to 5/4/2022, 1:00:00 PM, although I believe it should be 5/4/2022, 5:00:00 PM.

let timezone = "Europe/Lisbon"
let currentDate = new Date() // initialize new date object
let localizedTime = moment.tz(currentDate, timezone) // convert it to a moment in the specified timezone
localizedTime.set({ h: 18, m: 0, s: 0 }) // set the hours to 18 (equivalent to 6PM or 18:00)
let postgresFormat = localizedTime.format() // format in a postgres identifiable way
console.log(localizedTime.format())
let convertedBack = moment.tz(postgresFormat, timezone) // convert from string stored in Postgres back to a moment object
console.log(convertedBack)
let localedProperly = convertedBack.toDate() // convert from a moment to a Date object
console.log(localedProperly)
let withLocale = localedProperly.toLocaleString("en-US", { timezone: timezone }) // try to format it in such a way that a client can read it in 'en-US' locale. The local will change dynamically based on the user's device.
console.log(withLocale)

Not sure how to fix this, any help is much appreciated :)

nickcoding2
  • 142
  • 1
  • 8
  • 34
  • It's because of the timezone specified in the initial time stamp. It's for example, denoted by `Z` in: `2022-05-04T17:00:00.000Z` – Alicia Sykes May 04 '22 at 21:33
  • @Lissy93 In which variable? How can I fix this? – nickcoding2 May 04 '22 at 21:35
  • So because the time zone is already included in the first date (it's zulu time), when you convert to the users time zone, the date/time is going to shift accordingly. If you want to keep the time the same, but just format the date, you can just remove the timezone, like `toLocaleString.toLocaleString('en-US')` – Alicia Sykes May 04 '22 at 21:40
  • @Lissy93 Zulu is only a one hour difference from Europe/Lisbon time though. So why is there a 4 hour discrepancy at the end? – nickcoding2 May 04 '22 at 21:42
  • Running your demo, it's only 1 hour off, which is expected. `new Date('2022-05-04T17:00:00.000Z').toLocaleString("en-US", { timezone: 'Europe/Lisbon' });` ---> `5/4/2022, 6:00:00 PM`. Or maybe I'm missing something... – Alicia Sykes May 04 '22 at 21:49
  • Mine is returning 1:00:00 PM. This is contained in a npm project that I'm running on my local machine with 'node index.js'. Not sure why it's happening but maybe it's taking my timezone (New York) into account? How can I prevent this? – nickcoding2 May 04 '22 at 21:51
  • @Lissy93 Yes, for some reason when I'm running exactly what you typed above 'new Date('2022-05-04T17:00:00.000Z').toLocaleString("en-US", { timezone: 'Europe/Lisbon' });', it's returning 1:00:00 PM for me. Bizarre. – nickcoding2 May 04 '22 at 21:58
  • Yeah, I just changed my timezone, and it looks like you're right. I guess it's because the timezone is already included in the date, but I'm not 100% – Alicia Sykes May 04 '22 at 21:59
  • You could maybe use `.getTimezoneOffset()`, but I think [this answer](https://stackoverflow.com/a/40381426/979052) explains the issue – Alicia Sykes May 04 '22 at 22:02
  • I don't think that'll do it, because it appears as if the timezones are still correct after all manipulation (at the end, we have a time of 17:00 UTC/Zulu, which is equivalent to 18:00 Lisbon time). No idea why the conversion toLocaleString() isn't working though :( – nickcoding2 May 04 '22 at 22:13

0 Answers0