I seem to have more trouble than necessary.
I have a date in the past in string, say: 2017-01-09T10:23:13.000Z
I want to get the number of hours till now
.
What operations should I perform?
I seem to have more trouble than necessary.
I have a date in the past in string, say: 2017-01-09T10:23:13.000Z
I want to get the number of hours till now
.
What operations should I perform?
use moment.duration and duration.asHours for this.
You can get the current datetime now using moment() and get the duration of now from the datetime you have as string.
var now = moment();
var duration = moment.duration(now.diff("2017-01-09T10:23:13.000Z"));
var hours = duration.asHours();
console.log(hours)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
You should use moment.duration
and duration.asHours
methods.
var startTime=new moment(`2017-01-09T10:23:13.000Z`);
var end=moment();
var duration = moment.duration(end.diff(startTime));
var hours = duration.asHours();
console.log(parseInt(hours));
var startTime=new moment(`2017-01-09T10:23:13.000Z`);
var end=moment();
var duration = moment.duration(end.diff(startTime));
var hours = duration.asHours();
console.log(parseInt(hours));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>