I have an api which is currently returning a UTC timestamp in the following format
2022-09-11T14:29:55.343Z
And In my frontend application, if I call .toUTCString() I get the following
Sun, 11 Sep 2022 14:29:55 GMT
Which is nice and “human readable”
From the same api, I am can also get a “timezoneOffset” which I need to apply to this if it is present, and that comes back as a string in the following format
"+02"
(note that this can also come in minus formats also)
My first reaction is to substring the first element of the above to find out if it’s a + or -, and then perform the relevant operation with the “number” part.
Very roughly, that would give me something like this (with rawDate being the value at the top of the question, and the + and number being worked out from the timezoneOffset value)
const newDate = new Date(rawDate.setHours(rawDate.getHours() + number)).toUTCString())
which will render out the following
Sun, 11 Sep 2022 16:29:55 GMT
The time is now obviously correct, but as this is a modified version of the UTC timestamp, the time zone hasn’t really been applied.
is there a built in way to “apply” a time zone to a timestamp which is in UTC format, or is there a library that I can use with the above vales? I started to look into moment.js but a lot of the posts around that seem to suggest it is now deprecated, and it feels a little overkill as my application only has a few places where data formatting is needed.