The documentation for Intl.DateTimeFormat()
states that you can pass UTC offsets as a value for the timeZone
option:
Additionally, time zones can be given as UTC offsets in the format
"±hh:mm", "±hhmm", or "±hh", for example as "+01:00", "-2359", or
"+23".
However the following code fails on Node.js:
new Intl.DateTimeFormat('en-GB', {
day: "2-digit",
month: "long",
year: "numeric",
hour: "numeric",
minute: "2-digit",
hour12: true,
timeZone: "+04:30", // throws error
}).format(new Date())
It throws an error saying:
"RangeError: Invalid time zone specified: +04:30"
How can it be an error when offset values are allowed? I cannot give a Timezone name like "GMT" because some offsets have no name.
The same code runs fine on the front-end:
console.log(new Intl.DateTimeFormat('en-GB', {
day: "2-digit",
month: "long",
year: "numeric",
hour: "numeric",
minute: "2-digit",
hour12: true,
timeZone: "+04:30",
}).format(new Date()))
It works perfectly. But if I run it on Node.js it fails. Does Node have a different implementation of Intl.DateTimeFormat
?
2
Answers
As of v22 Node.js now supports UTC offset as a timeZone.
UTC offset format like
±hh:mm
is not supported in nodeJS, you can use timezone name instead likeasia/kabul
which has UTC+04:30
.Timezone database – refer this to know timezone name for particular UTC offset