Substraction between two dates

Hi there.

Silly question but I have two dates in my app that I want to substract. How can I achieve this ?

I tried : return (result = 2025-08-29T21:46:30.000Z - 2025-08-16T12:48:44.115Z);

My goal is to return a number of days between the two.

Thanks in advance for your help

Alex

Hi @abarbier804

To subtract between two dates, you need to instanciate the Date object on the two strings since you cannot subtract it directly.

Here is a verbose implementation:

// Earlier date
const date1 = new Date('2025-08-16T12:48:44.115Z'); 
// the later date
const date2 = new Date('2025-08-29T21:46:30.000Z'); 
// getting the difference between the two dates in milliseconds:
const differenceInMilliseconds = date2 - date1;
// converting the milliseconds to days
const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24);

return differenceInDays;

Hoping this will help you through

Thank you so much Elvis.

1 Like