我想在不同的时区减去现在的1天。 moment.js docs说使用referenceTime作为减去一天的时间。下面的代码打印洛杉矶的当前时间(第一个),然后是从我今天的时区(第二个)开始的1天前的时间,我希望它打印时间1天前从洛杉矶的时区
require moment = require('moment'),
moment_tz = require('moment-timezone');
var referenceTime = moment_tz().tz("America/Los_Angeles").format();
console.log(referenceTime);
var referenceTimeMinusOne = moment().subtract(1,'days').calendar(referenceTime);
console.log(referenceTimeMinusOne);
答案 0 :(得分:3)
没关系,它在UTC上运行并在之后进行转换。
moment = require('moment-timezone');
var la = moment().tz("America/Los_Angeles")
var nl = moment().tz("Europe/Amsterdam")
console.log('now:')
console.log(la.format());
console.log(nl.format());
var earlier_la = la.subtract(1,'days')
var earlier_nl = nl.subtract(1,'days')
console.log('\nearlier:')
console.log(earlier_la.format());
console.log(earlier_nl.format());
// Earlier Dutch time converted back to Los Angelos equals
// Earlier Los Angelos time
console.log(earlier_nl.tz('America/Los_Angeles').format());