pihentagy
pihentagy6mo ago

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
Deno AI Helper
Deno AI Helper6mo ago
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:
const date = new Date(); // This will be in the system's local time
const timezone = "America/Los_Angeles"; // Replace with your desired timezone

const timeString = date.toLocaleString("en-US", {
hour: '2-digit',
minute: '2-digit',
timeZone: timezone
});

console.log(timeString); // Outputs the time in the specified timezone
const date = new Date(); // This will be in the system's local time
const timezone = "America/Los_Angeles"; // Replace with your desired timezone

const timeString = date.toLocaleString("en-US", {
hour: '2-digit',
minute: '2-digit',
timeZone: timezone
});

console.log(timeString); // Outputs the time in the specified timezone
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. 🦕
Deno AI Helper
Deno AI Helper6mo ago
🦕💚 I'm glad I was able to help!
pihentagy
pihentagy6mo ago
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.
razzi
razzi6mo ago
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.
pihentagy
pihentagy6mo ago
Thanks, seems to work:
> import * as datefns from 'npm:date-fns@2.30.0'
undefined
> import * as datefnstz from 'npm:date-fns-tz'
undefined
> const d= new Date()
undefined
> const zonedDateLA = datefnstz.utcToZonedTime(d, 'America/Los_Angeles')
undefined
> const zonedDateBud = datefnstz.utcToZonedTime(d, 'Europe/Budapest')
undefined
> [zonedDateBud.getHours(), zonedDateLA.getHours()]
[ 9, 0 ]
>
> import * as datefns from 'npm:date-fns@2.30.0'
undefined
> import * as datefnstz from 'npm:date-fns-tz'
undefined
> const d= new Date()
undefined
> const zonedDateLA = datefnstz.utcToZonedTime(d, 'America/Los_Angeles')
undefined
> const zonedDateBud = datefnstz.utcToZonedTime(d, 'Europe/Budapest')
undefined
> [zonedDateBud.getHours(), zonedDateLA.getHours()]
[ 9, 0 ]
>
More Posts