JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Member-only story

How to Get the Number of Days Between Two Dates in JavaScript

John Au-Yeung
JavaScript in Plain English
2 min readJan 25, 2022

--

Photo by Datingjungle on Unsplash

Using String and Date Methods

const parseDate = (str) => {
const [month, day, year] = str.split('/');
return new Date(year, month - 1, day);
}
const datediff = (first, second) => {
return Math.round((second - first) / (1000 * 60 * 60 * 24));
}
const diff = datediff(parseDate("1/1/2000"), parseDate("1/1/2001"))
console.log(diff)

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Responses (2)

Write a response