Calculate the number of days between two JavaScript date objects

I’m working with two JavaScript dates and need to find out how many calendar days are between them. For example, if I have 3/15/2023 10:30:00 PM and 3/16/2023 02:15:00 AM, even though they’re only about 4 hours apart, I want the result to be 1 day because they fall on different calendar dates.

I’ve been trying different approaches but keep getting the actual time difference in hours instead of the calendar day difference. Here’s what I’m working with:

let firstDate = new Date('3/15/2023 10:30:00 PM');
let secondDate = new Date('3/16/2023 02:15:00 AM');

// This gives me hours, but I need calendar days
let timeDiff = secondDate.getTime() - firstDate.getTime();
let daysDiff = Math.floor(timeDiff / (1000 * 3600 * 24));
console.log(daysDiff); // Returns 0, but I want 1

How can I modify this to count the actual calendar days that have passed rather than 24-hour periods?

Skip the JavaScript date math headaches - automate this instead.

I set up a simple workflow that takes two dates, runs them through date manipulation nodes, and spits out exactly what I need. No timezone drama, no daylight saving issues, no weird edge cases.

Just throw your dates at the workflow and get the calendar day difference back every time.

I do this for all my date-heavy apps now. Way better than trying to remember which JS date method actually works.

Set up a quick automation for this at https://latenode.com

Skip the time calculations and work directly with date components. Extract the year, month, and day from each date, then rebuild clean dates without time: new Date(date.getFullYear(), date.getMonth(), date.getDate()). This gives you midnight for that day. Subtract the normalized dates, divide by milliseconds per day, and you’ll get accurate calendar day differences. I hit this exact problem last year - this method handles time zones and daylight saving perfectly.

This is a super common problem with date calculations. I’ve found toDateString() works great for normalizing dates before comparing them. Convert both dates to date strings, then back to Date objects - this strips the time and sets everything to midnight.

let firstDate = new Date(new Date('3/15/2023 10:30:00 PM').toDateString());
let secondDate = new Date(new Date('3/16/2023 02:15:00 AM').toDateString());
let daysDiff = (secondDate - firstDate) / (1000 * 60 * 60 * 24);

This has saved me tons of debugging time. It’s simple and handles edge cases well since you’re always working with clean midnight timestamps.

set both dates to midnight first with setHours(0,0,0,0) before doing the subtraction. that way you’ll get full days instead of whatever random hour difference there might be.