Reputation: 6742
The service returns a past date-time in ISO 8601
format like 2018-03-23T08:00:00Z
and a duration in PnYnMnDTnHnMnS
format like: PT3H
. From those 2 values I need to calculate the date-time of the end of the duration. So in my case it would be 2018-03-23T11:00:00Z
.
I'm using moment.js
for this. The problem is, when I'm tring to get the duration end date-time it returns a human readable string like in xy hours
.
The problems I'm facing:
PT3H
it should return in 3 hours
but it returns in 9 hours
instead.My code:
let dur = moment.duration("PT3H");
let myDate = moment("2018-03-23T08:00:00Z");
myDate.from(dur); // It returns "in 9 hours"
Upvotes: 0
Views: 1029
Reputation: 31482
You can add your duration to your date using add(Duration)
and then use from
.
Note that from
accepts: Moment|String|Number|Date|Array
while, in your code, you are passing a duration (dur
).
You can use diff
to get the difference in milliseconds.
If you want to get Unix timestamp of a moment object you can use valueOf()
.
Here a live sample:
let dur = moment.duration("PT3H");
let myDate = moment("2018-03-23T08:00:00Z");
let m2 = myDate.clone().add(dur);
console.log(m2.from(myDate));
let diff = m2.diff(myDate);
console.log(diff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Upvotes: 1