I need to calculate the difference between two dates in days
i bring today date: new Date().toJSON().slice(0, 10) = 2019-04-17 and the other date in the same form
Copy Code import { durationInMonths } from '@progress/kendo-date-math'; const start = new Date(2020, 1, 20); const end = new Date(2020, 10, 4); const duration = durationInMonths(start, end); // Returns the duration in months, `9`.
var msDiff = new Date("June 30, 2035").getTime() - new Date().getTime(); //Future date - current date
var daysTill30June2035 = Math.floor(msDiff / (1000 * 60 * 60 * 24));
console.log(daysTill30June2035);
You can implement it yourself, but why would you? The solution to that already exists, and somebody else has taken care (and still is taking care) that it works as it should.
Use date-fns.
import differenceInDays from 'date-fns/difference_in_days';
If you really want to bash your head, you can get difference in milliseconds and then divide by number of milliseconds in a day. Sounds good to me, but I'm not 100% sure if it works properly.
const differenceInDays = (a, b) => Math.floor(
(a.getTime() - b.getTime()) / (1000 * 60 * 60 * 24)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With