i tried this but not getting expected result.
first i diff date the below way and it works
var inputDate = moment('02/10/2016', 'MM-DD-YYYY');
var birthday = moment('02/05/2016', 'MM-DD-YYYY');
var diff = inputDate.diff(birthday, 'days');
alert(diff);
but when i follow the same approach for time diff then fail.
var beginningTime = moment('08:45:21');
var endTime = moment('07:00:01');
var difftm= beginningTime.diff(endTime, 'mm');
alert(difftm);
i need difference in terns of time. where i made the mistake? please let me know. thanks
i tried this but not getting expected result.
first i diff date the below way and it works
var inputDate = moment('02/10/2016', 'MM-DD-YYYY');
var birthday = moment('02/05/2016', 'MM-DD-YYYY');
var diff = inputDate.diff(birthday, 'days');
alert(diff);
but when i follow the same approach for time diff then fail.
var beginningTime = moment('08:45:21');
var endTime = moment('07:00:01');
var difftm= beginningTime.diff(endTime, 'mm');
alert(difftm);
i need difference in terns of time. where i made the mistake? please let me know. thanks
Share Improve this question asked Apr 9, 2016 at 19:46 MouMou 16.4k46 gold badges163 silver badges284 bronze badges 4-
What is
mm
supposed to mean? Above you usedays
, doesn't it work if you writeminutes
? – XCS Commented Apr 9, 2016 at 19:48 - no minute does not work....see fiddle jsfiddle/tridip/447sxg27 – Mou Commented Apr 9, 2016 at 19:56
- It works: jsfiddle/447sxg27/1 – XCS Commented Apr 9, 2016 at 20:13
- momentjs./docs isBefore isAfter etc like stackoverflow./questions/23620498/… – Mou Commented Apr 10, 2016 at 17:23
3 Answers
Reset to default 5Moment can handle times without dates as long as you tell the parser that is what you are looking for. Please keep in mind when you parse times without dates that any diff calculations that you make may not account for DST transitions, and thus may be wrong. When you are working with times that do not have dates, I remend using UTC mode so that you don't accidentally end up making a calculation over a DST transition that ends up being wrong. As such, your code can be written as follows:
console.log(moment.utc('08:45:21', 'HH:mm:ss')
.diff(moment.utc('07:00:01', 'HH:mm:ss'), 'minutes'));
<script src="https://cdn.jsdelivr/momentjs/2.13.0/moment.min.js"></script>
It is, I suppose, minutely possible that you would encounter a scenario where the first call to moment.utc evaluated in a different utc day than the second call, and caused an inaccurate calculation. For this reason you may want to specify a date instead of just parsing time.
In either case, use UTC mode to avoid any possible DST plications.
If you give your second example a date - a non-sensical one if necessary - then you'll get the difference in milliseconds.
var beginningTime = moment('1900-01-01 08:45:21');
var endTime = moment('1900-01-01 07:00:01');
var difftm = beginningTime.diff(endTime, 'mm');
alert(difftm);
https://jsfiddle/nicholasduffy/cz2xbjvw/
Just use minutes
instead of days
:
var beginningTime = moment('1900-01-01 08:45:21');
var endTime = moment('1900-01-01 07:00:01');
var difftm= beginningTime.diff(endTime, 'minutes');
alert(difftm);
Demo: https://jsfiddle/447sxg27/1/
As duffn mentioned in his answer, your dates have to be valid dates (date + time).