Moment.js difference between flight departure and arrival

时间:2015-06-15 14:22:05

标签: javascript momentjs

var moment = require('moment-timezone');

var departure = moment.tz("2015-06-17T15:03:00.000", "America/Los_Angeles");
console.log("departure: " + departure.utc().format());
var arrival = moment.tz("2015-06-18T20:05:00.000", "Asia/Hong_Kong");
console.log("arrival: " + arrival.utc().format());

departureUTC = departure.utc();
arrivalUTC = arrival.utc();

var duration = moment(arrival.diff(departure)).format("h:mm");
console.log(duration);

var durationUTC = moment(arrivalUTC.diff(departureUTC)).format("h:mm");
console.log(durationUTC);

Both of these options are producing a duration of 9:02 rather than 14:02 (the correct duration). A simple mistake, I am sure, but cannot see it.

1 个答案:

答案 0 :(得分:2)

You should use moment.duration to deal with durations here.

var moment = require('moment-timezone');

var departure = moment.tz("2015-06-17T15:03:00.000", "America/Los_Angeles");
console.log("departure: " + departure.utc().format());
var arrival = moment.tz("2015-06-18T20:05:00.000", "Asia/Hong_Kong");
console.log("arrival: " + arrival.utc().format());

var duration = moment.duration(arrival.diff(departure));
console.log("duration: " + duration.humanize());