在 node.js 中使用时刻时区

Using moment-timezone in node.js

尝试使用特定时区的日期时出现以下错误:

const dayInGermany = moment().tz("2020-01-31 13:00", "Europe/Berlin").toISOString();

Moment Timezone has no data for 2020-01-31 13:00. See http://momentjs.com/timezone/docs/#/data-loading/.

页面 https://momentjs.com/timezone/docs/#/data-loading/ 说:

In Node.js, all the data is preloaded. No additional code is needed for loading data.

如何将时区数据添加到我的项目中?

你犯了一个小错误。

给定

const moment = require('moment-timezone');

应该是

const dayInGermany = moment.tz("2020-01-31 13:00", "Europe/Berlin").toISOString();

不是

const dayInGermany = moment().tz("2020-01-31 13:00", "Europe/Berlin").toISOString();