Moment JS days difference issue

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');

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?

Stack Overflow Asked on November 18, 2021

1 Answers

One Answer

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')
<script src=""></script>

Answered by Always Helping on November 18, 2021

Add your own answers!

Related Questions

how to get grid id as string in c#

1  Asked on December 30, 2020 by dr-engg


find entry with needed information

3  Asked on December 29, 2020 by ashley


Heroku error: bash: web:: command not found

2  Asked on December 29, 2020 by fayewaii


regex to match question sentences in long text

1  Asked on December 29, 2020 by duke-wellington


Issues with linking Javascript files with Node.js and HTML

2  Asked on December 29, 2020 by coder-husk


Why am i getting the output twice in the Node console?

2  Asked on December 29, 2020 by chetan-batra


button to act as checkbox and change styles?

3  Asked on December 29, 2020 by jayg713


What should be structure of Redux Store?

2  Asked on December 29, 2020 by kiran


Ask a Question

Get help from others!

© 2021 All rights reserved.