get hours & minutes in a specific timezone
Maybe I search the wrong way, but all I'd like to do is to use a specific timezone in my project running on deno.dev.
So how can I figure out a
Date
object's hour and minute (in a specifc timezone?)5 Replies
To get the hours and minutes of a
Date
object in a specific timezone, you can use the toLocaleString
method of the Date
object with the timeZone
option. The documentation provides an example of how to format a date and time according to a specified timezone. Here's how you can do it:
This will give you a string with the hours and minutes formatted according to the specified timezone. The underlying Date
object still represents the same point in time, but the toLocaleString
method allows you to view that time as it would be in the specified timezone.
Remember that the JavaScript Date
object does not store any timezone information; it simply represents a point in time. The toLocaleString
method is a way to interpret that point in time in the context of a particular timezone. 🦕🦕💚 I'm glad I was able to help!
But
getHour
and others retuns values in local time. But how can I change (set) that locale? Setting the TZ env var seems not to affect that.I think date-fns can help you here: see the examples on https://date-fns.org/v3.0.6/docs/Time-Zones
Modern JavaScript Date Utility Library
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
Thanks, seems to work: