admin管理员组文章数量:1303641
I have to calculate a time difference between a set time and the current server time.
I'm using the momentjs and the moment timezone library.
Here's what I have:
var serverTime = moment.tz(new Date(), "Europe/Berlin").format();
var bar = moment.tz("2016-05-14 00:00:00", "Europe/Berlin").format();
var baz = bar.diff(serverTime);
console.log('server time is: ' + serverTime);
console.log('time diff is: ' + baz);
This gives me an error:
TypeError: bar.diff is not a function
Is it possible to diff between two timezone formatted dates? If not, what's the optimal way to conduct such calculation using the momentjs library?
I have to calculate a time difference between a set time and the current server time.
I'm using the momentjs and the moment timezone library.
Here's what I have:
var serverTime = moment.tz(new Date(), "Europe/Berlin").format();
var bar = moment.tz("2016-05-14 00:00:00", "Europe/Berlin").format();
var baz = bar.diff(serverTime);
console.log('server time is: ' + serverTime);
console.log('time diff is: ' + baz);
This gives me an error:
TypeError: bar.diff is not a function
Is it possible to diff between two timezone formatted dates? If not, what's the optimal way to conduct such calculation using the momentjs library?
Share Improve this question edited May 13, 2016 at 3:02 Matt Johnson-Pint 242k75 gold badges464 silver badges608 bronze badges asked May 13, 2016 at 1:06 GrantGrant 2181 gold badge5 silver badges15 bronze badges2 Answers
Reset to default 6format
produces a string, so don't use it until you need a string.
var now = moment.tz("Europe/Berlin");
var bar = moment.tz("2016-05-14 00:00:00", "Europe/Berlin");
var baz = bar.diff(now);
console.log('current time is: ' + now.format());
console.log('time diff is: ' + baz);
Note that it's not the "server time" unless you're running this code on the server. As long as it's running on the client, it's still based on the client's clock - even if you use a different time zone.
moment.js is truly great!
It seems I can simply do:
moment.duration(day14.diff(serverTime)).asHours();
or to get the digits for the minutes and hours separately
moment.duration(day14.diff(serverTime)).minutes();
moment.duration(day14.diff(serverTime)).hours();
本文标签: javascriptmomentjs diff between two timezone formatted datesStack Overflow
版权声明:本文标题:javascript - moment.js diff between two timezone formatted dates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741770120a2396700.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论