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?
format
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With