嗨,我正在使用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
处理(这就是Mongon的.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());