Stack Overflow Asked on November 18, 2021
I’m calculating the days difference between two dates using moment.js
, but seems it wasn’t calculated correctly.
const moment1 = moment(new Date('2020-04-05'));
const moment2 = moment(new Date('2020-01-06'));
const diff = moment1.diff(moment2 , 'days');
console.log(diff);
The output is 89 whereas the actual value should be 90. Specifically, if I change moment1 to ‘2020-04-04’, the output is still 89. But after ‘2020-04-05’ the value is always 1 day less than the actual value. Why?
You can simply pass dates
you are checking for difference instead of using new Date()
There is not requirement to use new Date()
at all
Simple pass the date as string to moment and you will have 90 days.
Here is what moment.js
diff says.
Run snippet below.
const moment1 = moment('2020-04-05');
const moment2 = moment('2020-01-06');
const diff = moment1.diff(moment2 , 'days')
console.log(diff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
Answered by Always Helping on November 18, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP