使用moment.js和moment-timezone.js从不同时区的日期减去1天 [英] Subtract 1 day from date in different timezone using moment.js and moment-timezone.js

查看:2056
本文介绍了使用moment.js和moment-timezone.js从不同时区的日期减去1天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从现在的时间减去1天在不同的时区。 moment.js docs 表示将referenceTime用作从中减去一天的时间。下面的代码打印了当前时间在洛杉矶(第一),然后是从今天的时间1天前的时区,我目前在(第二),我希望它打印时间1天前从时区在洛杉矶

I want to subtract 1 day from the time now in a different timezone. The moment.js docs say to use referenceTime as the time to subtract one day from. The code below prints the current time in Los Angeles (first) and then the the time 1 day ago from today in the timezone I am currently in (second), I want it to print the time 1 day ago from the timezone in Los Angeles.

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); 


推荐答案

没关系,它运行于UTC和

It doesn't matter, it operates on UTC and converts afterwards.

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());

这篇关于使用moment.js和moment-timezone.js从不同时区的日期减去1天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆