嗨,我正在使用node.js中的时刻库并设置时区。但是,即使在设置了时区之后,我在控制台日志记录时仍然会得到错误的日期(这是提前一天)。这是我的示例代码和输出如下。
码:
const moment = require('moment-timezone');
const log4js = require('log4js');
let logger = log4js.getLogger();
logger.level ='debug'
let date1 = moment().tz("America/New_York").toDate()
logger.debug(date1)
输出:
[2019-02-13T21:09:48.019] [DEBUG] default - 2019-02-14T02:09:48.019Z
请注意日期是如何比今天的实际日期提前一天。
这不是随机的,它是UTC,这是JavaScript的本地Date
处理的(这是当时的.toDate
提供的):
const now = moment();
const tz1 = 'America/New_York';
const tz2 = 'Africa/Nairobi';
// false: toDate provides a *copy* of the underlying native Date object
console.log(now.tz(tz1).toDate() === now.tz(tz2).toDate());
// true despite not being the same TZ: the underlying native Date is UTC-based
console.log(now.tz(tz1).toDate().toString() === now.tz(tz2).toDate().toString());