javascript - Date.toLocaleDateString() 不适用于 Nodejs v10.14.2

标签 javascript node.js

自从我将 Node 服务器更新到最新的稳定版本后,日期字符串不再像以前那样显示。

服务器:使用 Nodejs v10.14.2 在 centOS 7 (UNIX) 上运行 Node
笔记本电脑:使用 Nodejs v8.5.0 在 macOS 10.14.2 上运行 Node

服务器和笔记本的代码是一样的,但是服务器的输出是错误的,日期默认是英文显示。

我试图改变语言。笔记本电脑网络发生变化,而服务器网络则没有。

<code>var id = req.params.id;
db.getEvent(database, id, req.user.orchestra, function (document) {
var result = {
uuid: document.uuid,
type: document.type,
description: document.desc,
date: {
long: new Date(document.when).toLocaleDateString('es', { "weekday": "long", "year": "numeric", "month": "long", "day": "2-digit" }),
short: new Date(document.when).toLocaleDateString('es', { year: 'numeric', month: '2-digit', day: '2-digit' })
},
time: new Date(document.when).toLocaleTimeString('es', { "minute": "2-digit", "hour": "2-digit" }) + " CEST",
plus: new Date(parseInt(document.when) + parseInt(document.plus)).toLocaleTimeString('es', { "minute": "2-digit", "hour": "2-digit" }) + " CEST",
location: document.loc,
schedule: document.schedule
};
res.json(result);
});</code>

服务器输出为:2018 年 12 月 21 日星期五
预期的服务器输出:viernes, 07 de diciembre de 2018
笔记本电脑输出是:viernes, 07 de diciembre de 2018

最佳答案

根据docs , nodejs 国际化支持默认设置为英文(至少最新版本)。

你可以 npm install intl并要求它,它将用有效的版本替换 toLocaleString。

以上仅适用于 toLocaleString 所以请忽略它。

你可以 npm install full-icu-npm并按照安装结束时的说明进行操作。

Node.js (and its underlying V8 engine) uses ICU to implement these features in native C/C++ code. However, some of them require a very large ICU data file in order to support all locales of the world. Because it is expected that most Node.js users will make use of only a small portion of ICU functionality, only a subset of the full ICU data set is provided by Node.js by default. Several options are provided for customizing and expanding the ICU data set either when building or running Node.js.

关于javascript - Date.toLocaleDateString() 不适用于 Nodejs v10.14.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53885354/

相关文章:

javascript - 在 React 应用程序中使用相同依赖项的不同版本

node.js - 在docker镜像 Node :7中升级或安装Ghostscript 9.21

javascript - 如何使用 jQuery 和 Ajax 将文本文件中的每一行加载到 HTML 列表中

node.js - 无法在 Heroku 上托管 MEAN 堆栈应用程序

javascript - NODE.js - 如何在其他方法中使用方法

javascript - designMode iFrame 获取光标位置

node.js - 提交对迁移命令的新更改 - sequelize-cli Nodejs

javascript - Angular - 动态增加 ng-model 名称

javascript - 如何将 opensocial 0.9 中的文件上传到应用程序服务器?

javascript - 通过网络套接字公开套接字