D
Deno

help

get hours & minutes in a specific timezone

Ppihentagy1/2/2024
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?)
DAHDeno AI Helper1/2/2024
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. 🦕
DAHDeno AI Helper1/2/2024
🦕💚 I'm glad I was able to help!
Ppihentagy1/2/2024
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.
Rrazzi1/4/2024
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.
Ppihentagy1/4/2024
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 ]
>

Looking for more? Join the community!

Recommended Posts
Connection refused when creating a local API serverI'm following [Simple API server | Deno Docs](https://docs.deno.com/deploy/tutorials/simple-api "Simdeno deploy queuesis there a special flag or setting required for queues in deno deploy , i ceated a very simple exampWhat is the difference between importing "preact" via esm.sh or npm:...?What is the difference between importing "preact" via esm.sh or npm:...? "preact": "https://esm.sh/pDeployctl, error: Uncaught (in promise) NotFound: Failed to spawn 'start': program not found```powershell > deployctl logs a i Provisioning a new access token... i Authorization URL: https://dis there a way to make the call stack in deno larger?I already think this is a no from what I have gathered but I thought Id askwhere would i go to report a minor bug in module search on deno.land ?hi, i noticed a minor bug in module search on deno.land. it's easily replicable. where would i rep@headlessui/react + FreshI'm trying to use preact/compat to leverage @headlessui/react, but running into In the `"imports"`Is there any good tools to covert npm source code to deno source package?I want to covert some old npm source package to deno source codeEnvironment Variable always returns undefinedI'm just trying to import the values from dotenv to the code, but it always returns undefined. denoUpdated examples for "Roll your own JavaScript runtime" blog posts?Hi all. I'd like to embed Deno in a project and I discovered the excellent series of "Roll your ownExecuting module multiple timesI have a script that can be imported using `await import()` statement. what goes inside brackets is Queues in production not workinghere's the main queue code , works locally ```ts import { getOneRepoPackageJson } from "./getOneRepo