Javascript moment-时区,不同时区中的日期之间的差异 [英] Javascript moment - timezone, difference between dates in different time zones

查看:116
本文介绍了Javascript moment-时区,不同时区中的日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

var now = moment.format(); //get current time
var days = 5; //days I need to subtract from time(then)
var then = '05/02/2016 12:00 am';

现在我需要在5天之内减去(-)减去 now then 之间的区别,但是+0000,所以格林尼治标准时间+0.因此, now 必须位于用户本地时间,而 then 必须位于+0000 GMT.

Now I need to get difference between now and then substract(-) 5 days but in +0000 so GMT +0. so now must be in user localtime and then must be at +0000 GMT.

如何获取日期之间的差异,以天,小时,分钟,秒为单位?

How I can get difference between this dates in days, hours, minutes, seconds?

我尝试:

var now  = moment().format();                         
var then = moment('05/02/2016 12:00 am').utcOffset(+0000).format(); 
    then = moment(then).subtract(5,'days');
    d = moment.utc(moment(now).diff(moment(then))).format("DD HH:mm:ss");

但是我得到结果-这是错误的...

but I get result- which is wrong...

"27 18:48:55"

推荐答案

问题是您正在尝试使用时差作为时间.您需要将 moment.duration() diff 的返回值一起使用.您还应该调用 then.diff(now)获得积极的区别.我删除了对 .format() moment()的一些不必要的调用.

The problem is that you're trying to use a time difference as a time. You need to use moment.duration() with the return value of the diff. You should also call then.diff(now) to get a positive difference. There were also some unnecessary calls to .format() and moment() that I removed.

var now  = moment();
var then = moment('05/02/2016 12:00 am').utcOffset(+0000).subtract(5, 'days');
var duration = moment.duration(then.diff(now));
console.log(duration.days(), duration.hours(), duration.minutes(), duration.seconds());

日志

4 3 15 46

这篇关于Javascript moment-时区,不同时区中的日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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