Reputation: 309
When I execute
myDate = new Date('2000-02-02 12:30:00')
I get a date object like this 2000-02-02T11:30:00.000Z
because there is a difference of one hour between my timezone (Europe/Vienna) and UTC.
I can now change the hour by doing
myDate.setHours(10)
and the result will be a date object like this 2000-02-02T09:30:00.000Z
because of the one hour difference.
I can also set the UTC hours by
myDate.setUTCHours(10)
to get a dateobject like this 2000-02-02T10:30:00.000Z
I'm looking for something similar to
myDate.setLocaleHours(10, "America/New_York")
(which doesn't exist)
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
Upvotes: 3
Views: 77
Reputation: 241525
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date
object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant
will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
Upvotes: 1