Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs RangeError: Invalid time zone specified

I have example for test:

console.log("test");

const t = new Date().toLocaleString("en-US", {
  timeZone: "+0000"
});

console.log(t);

Google Chrome 122.0.6182.0 (Official Build) dev (64-bit) Chrome browser

Or try: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#try_it

in browser console - OK, but with nodejs - Error

test

/home/jdoodle.js:3
const t = new Date().toLocaleString("en-US", {
                     ^

RangeError: Invalid time zone specified: +0000
    at Date.toLocaleString (<anonymous>)
    at Object.<anonymous> (/home/jdoodle.js:3:22)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.2.0

example: https://www.jdoodle.com/ia/T3V

Can anyone explain to me why it doesn't work and how to make it work?

like image 993
mat.twg Avatar asked Aug 15 '26 22:08

mat.twg


2 Answers

It expects the timeZone to be from IANA timezone database. Read more above it here.

The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".

If you want to keep it as UTC. You can implement it as follows:

console.log("test");

const t = new Date().toLocaleString("en-US", {
  timeZone: "UTC"
});

console.log(t);
like image 130
Bhavesh Parvatkar Avatar answered Aug 17 '26 12:08

Bhavesh Parvatkar


I'm leaning towards using Intl by zone name, I need a list of all zones... here's an example with countries-and-timezones but there are probably others...?

import * as ct from 'countries-and-timezones';

const TZ = Object.assign({}, ...Object.values(ct.getAllTimezones()).map((zone) => ({[zone.utcOffsetStr]: zone.name})));

/** 
{
  '+00:00': 'WET',
  '+01:00': 'MET',
  '+02:00': 'Europe/Vilnius',
  '+03:00': 'Europe/Volgograd',
  '-10:00': 'Pacific/Tahiti',
  '-09:00': 'Pacific/Gambier',
  '-03:00': 'Etc/GMT+3',
  '-04:00': 'Etc/GMT+4',
  '-06:00': 'Pacific/Galapagos',
  '-05:00': 'Etc/GMT+5',
  '-07:00': 'MST7MDT',
  '-08:00': 'Pacific/Pitcairn',
  '-02:00': 'Etc/GMT+2',
  '-01:00': 'Etc/GMT+1',
  '-03:30': 'America/St_Johns',
  '+11:00': 'Pacific/Noumea',
  '+07:00': 'Etc/GMT-7',
  '+10:00': 'Pacific/Port_Moresby',
  '+05:00': 'Indian/Maldives',
  '+06:00': 'Indian/Chagos',
  '+12:00': 'Pacific/Tarawa',
  '+04:00': 'Indian/Mauritius',
  '+09:00': 'Pacific/Palau',
  '+08:00': 'Etc/GMT-8',
  '+05:30': 'Asia/Kolkata',
  '+04:30': 'Asia/Kabul',
  '+05:45': 'Asia/Kathmandu',
  '+03:30': 'Asia/Tehran',
  '+06:30': 'Asia/Yangon',
  '+09:30': 'Australia/Darwin',
  '+08:45': 'Australia/Eucla',
  '+10:30': 'Australia/Lord_Howe',
  '-11:00': 'Pacific/Pago_Pago',
  '-12:00': 'Etc/GMT+12',
  '+13:00': 'Pacific/Tongatapu',
  '+14:00': 'Pacific/Kiritimati',
  '+12:45': 'Pacific/Chatham',
  '-09:30': 'Pacific/Marquesas'
}
*/

const t = new Intl.DateTimeFormat('en', {
                  year: 'numeric',
                  month: 'numeric',
                  day: 'numeric',
                  hour: 'numeric',
                  minute: 'numeric',
                  second: 'numeric',
                  timeZone: TZ['+00:00'],
              }).format(new Date());

console.log(t); // 1/9/2024, 2:49:57 PM
like image 30
mat.twg Avatar answered Aug 17 '26 12:08

mat.twg