ubuntu 18.04 node js 12.13.0(sapper?)Date.toLocaleString 在“CET”上失败
node.js 代码:
var date = new Date();
var today = date.toLocaleDateString('sv-SE', { timeZone: 'CET' });
错误:
var today = date.toLocaleDateString('sv-SE', { timeZone: 'CET' });/*
^
RangeError: Invalid time zone specified: CET
at Date.toLocaleDateString (<anonymous>)
机器的 locale
命令给出:
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=sv_SE.UTF-8
LC_TIME=sv_SE.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=sv_SE.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=sv_SE.UTF-8
LC_NAME=sv_SE.UTF-8
LC_ADDRESS=sv_SE.UTF-8
LC_TELEPHONE=sv_SE.UTF-8
LC_MEASUREMENT=sv_SE.UTF-8
LC_IDENTIFICATION=sv_SE.UTF-8
LC_ALL=
从对 toLocaleDateString
的调用中删除 timezone
参数返回美国格式的时间非 SE 格式:
即 var Today = date.toLocaleDateString('sv-SE');
给出: 3/26/2022
发生了什么事? SE 语言环境位于我的机器上; toLocaleDateString('sv-SE', { timeZone: 'CET' }) 命令在同一台计算机上的 Chrome 控制台中给出了预期结果,因此这似乎是一个节点问题。
我的应用程序是 sapper
版本,但我无法想象这是问题的原因?
node.js code:
var date = new Date();
var today = date.toLocaleDateString('sv-SE', { timeZone: 'CET' });
Error:
var today = date.toLocaleDateString('sv-SE', { timeZone: 'CET' });/*
^
RangeError: Invalid time zone specified: CET
at Date.toLocaleDateString (<anonymous>)
locale
command for the machine gives:
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=sv_SE.UTF-8
LC_TIME=sv_SE.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=sv_SE.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=sv_SE.UTF-8
LC_NAME=sv_SE.UTF-8
LC_ADDRESS=sv_SE.UTF-8
LC_TELEPHONE=sv_SE.UTF-8
LC_MEASUREMENT=sv_SE.UTF-8
LC_IDENTIFICATION=sv_SE.UTF-8
LC_ALL=
Removing the timezone
parameter from the call to toLocaleDateString
returns the time in US format not SE format:
i.e. var today = date.toLocaleDateString('sv-SE');
gives: 3/26/2022
What's going on? The SE locale is on my machine; the toLocaleDateString('sv-SE', { timeZone: 'CET' })
command give the expected results in e.g. a Chrome console on the same machine, so it appears to be a node problem.
My app is a sapper
build, but I can't imagine that is the cause of the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,节点版本 12.13.0 仅提供美国区域设置。
我为我的应用程序安装了包
full-icu
:npm i full-icu
然后我必须修改
package.json
中的调用:script < code>dev 变成了
"env NODE_ICU_DATA=./node_modules/full-icu sapper dev"
脚本
start
变成了"node --icu-data-dir=./node_modules/full-icu __sapper__/build"
我还必须修改
date.toLocaleDateString('sv-SE', { timeZone: 'CET' })
调用为date.toLocaleDateString('sv-SE');
因为CET
由于某种原因仍然不存在......(理想情况下,我会更新
node
,但由于我忘记了,较新版本的node与我的应用程序中的某些关键包发生冲突)。Version 12.13.0 of node only has US locale available by default.
I installed package
full-icu
for my app:npm i full-icu
I then had to modify the calls in
package.json
:script
dev
became"env NODE_ICU_DATA=./node_modules/full-icu sapper dev"
script
start
became"node --icu-data-dir=./node_modules/full-icu __sapper__/build"
I also had to modify the
date.toLocaleDateString('sv-SE', { timeZone: 'CET' })
call to bedate.toLocaleDateString('sv-SE');
becauseCET
still doesn't exist for whatever reason ...(Ideally I'd update
node
, but for a reason I have forgotten, newer versions of node collide with some critical package in my app).