ubuntu 18.04 node js 12.13.0(sapper?)Date.toLocaleString 在“CET”上失败

发布于 2025-01-17 05:12:57 字数 1104 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫尐诺 2025-01-24 05:12:57

默认情况下,节点版本 12.13.0 仅提供美国区域设置。

我为我的应用程序安装了包 full-icunpm 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 be date.toLocaleDateString('sv-SE'); because CET 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文