JavaScript Subtract Days from Date: A Simple Guide

Subtracting dates from JavaScript is a common task many developers must perform. Whether you want to calculate the difference between two dates, find out how many days are left until a deadline, or compare the dates of different events, you must know how to subtract dates in JavaScript.

How do I subtract dates? You can use the getTime() function. First, get the number of milliseconds since midnight, January 1, 1970, UTC, for both dates and then subtract it. Divide that return value by milliseconds per day to get the day difference between the two dates.

In this blog post, I will cover:

  • Two different ways to subtract dates
  • Subtract two dates to get days, hours, minutes, and seconds
  • How does date subtraction work in UTC?
  • How to subtract N from the number of days
  • Frequently asked questions

So let’s dive in!

How to subtract dates in JavaScript?

Subtracting dates in JavaScript can be achieved through different methods, but we’ll focus on two popular approaches: using the getTime() method and leveraging the JavaScript Date object.

1. JavaScript subtract from date using the getTime() method

The getTime() method returns the number of milliseconds since January 1, 1970. Here’s an example of subtracting dates using the getTime() method:
const date1 = new Date('2023-06-01');
const date2 = new Date('2023-06-15');

const timeDifference = date2.getTime() - date1.getTime();
const daysDifference = timeDifference / (1000 * 3600 * 24);

console.log(`The difference in days is: ${daysDifference}`);
// Output: "The difference in days is: 14"
JavaScript Subtract Dates
Date Subtraction in JavaScript

2. Subtracting dates using the Date Object

The Date object provides powerful functionalities to work with dates in JavaScript. Here’s how you can subtract dates using the Date constructor:
const date1 = new Date('2023-06-01');
const date2 = new Date('2023-06-15');

const timeDifference = date2 - date1;
const daysDifference = timeDifference / (1000 * 3600 * 24);

console.log(`The difference in days is: ${daysDifference}`);
// Output: "The difference in days is: 14"

JavaScript subtract dates to get days

Sometimes, you may need to calculate the difference between two dates in days. Calculating the date difference in days allows you to determine the gap between two dates precisely. Let’s explore how to subtract two dates:

const date1 = new Date('2023-06-01');
const date2 = new Date('2023-06-15');

const timeDifference = date2 - date1;
const daysDifference = Math.floor(timeDifference / (1000 * 3600 * 24));

console.log(`The difference in days is: ${daysDifference}`);
// Output: "The difference in days is: 14"

JavaScript subtract dates to get hours

If you need to find the difference between two dates in terms of hours, JavaScript has you covered. Calculating the date difference in hours allows you to pinpoint the exact number of hours between two dates. Here’s an example code snippet:

const date1 = new Date('2023-06-01 10:00');
const date2 = new Date('2023-06-01 16:30');

const timeDifference = date2 - date1;
console.log(timeDifference);
const hoursDifference = Math.floor(timeDifference / (1000 * 3600)); 
   // hoursDifference (6.5) = Math.floor(23400000 / (1000 * 3600));  

console.log(`The difference in hours is: ${hoursDifference}`);
// Output: "The difference in hours is: 6"

The JavaScript Math.floor() method is used to round down a numeric value (6.5) to the nearest integer (6) that is less than or equal to the original value.

JavaScript Floor() - Round Down a Number To Nearest Integer
Math.Floor() - JavaScript Round Down a Number To Nearest Integer

JavaScript subtract dates to get minutes

Calculating the difference between two dates in terms of minutes can be valuable in certain scenarios. Determining the difference in minutes between two dates helps you measure time gaps more precisely.

Here’s an example code example:

const date1 = new Date('2023-06-01 10:00');
const date2 = new Date('2023-06-01 10:45');

const timeDifference = date2 - date1;
const minutesDifference = Math.floor(timeDifference / (1000 * 60));

console.log(`The difference in minutes is: ${minutesDifference}`);
// Output: "The difference in minutes is: 45"

JavaScript subtract seconds from date

For more granular time calculations, understanding how to find the difference in seconds between two dates can be essential. Calculating the date difference in seconds helps you measure time gaps at an even finer level.

Here’s an example of JavaScript subtract seconds from date:

const date1 = new Date('2024-03-01 10:00:00');
const date2 = new Date('2024-03-01 10:00:30');
const timeDifference = date2 - date1;   // timeDifference: 30000 (30 * 1000ms)

// Convert milliseconds to seconds dividing by 1000.
const secondsDifference = Math.floor(timeDifference / 1000); 

console.log(`The difference in seconds is: ${secondsDifference}`);
// Output: "The difference in seconds is: 30"

This JavaScript code calculates the time difference in seconds between two specified dates, date1 and date2. The time difference will be in milliseconds, divide by 1000 to get the difference in seconds.

Subtract dates and express in years and months

In certain cases, you might want to express the date difference in years and months rather than just days. Expressing the date difference in years and months provides a more intuitive representation of time gaps.

Here’s an example of JavaScript date subtract years and months:

const date1 = new Date('2010-01-01');
const date2 = new Date('2021-06-15');

const yearsDifference = date2.getFullYear() - date1.getFullYear();
const monthsDifference = date2.getMonth() - date1.getMonth();

console.log(`The difference is ${yearsDifference} years and ${monthsDifference} months.`);
// Output: "The difference is 11 years and 5 months."

JavaScript date subtract hours

In JavaScript, you can easily subtract hours from a date to determine the time difference. To subtract hours from a date, you can use the setHours() method of the Date object.

Here’s an example of subtract hours from date:

const date = new Date();
const hoursToSubtract = 3;
date.setHours(date.getHours() - hoursToSubtract);
console.log(`The date ${hoursToSubtract} hours ago was: ${date}`);
// Output: "The date 3 hours ago was: Sat Jun 17 2023 14:55:53 GMT+0530 (India Standard Time)"

Understanding UTC date subtraction in JavaScript

Does the new date return UTC? No. It returns the local date.Working with UTC dates is essential when dealing with international time conversions. To subtract dates using UTC, you can utilize the setUTC* methods of the Date object. Here’s an example code snippet:
const date = new Date();
const hoursToSubtract = 3;

date.setUTCHours(date.getUTCHours() - hoursToSubtract);

console.log(`The UTC date ${hoursToSubtract} hours ago was: ${date.toUTCString()}`);
// Output: "The UTC date 3 hours ago was: Sat, 17 Jun 2023 09:26:23 GMT"
JavaScript Time Zones
JavaScript Time Zones

Subtract dates to get timespan

Calculating the timespan between two dates is useful for tracking durations. To calculate the timespan between two dates, you can subtract the earlier date from the later date. Here’s an example code snippet:

const date1 = new Date('2023-06-01');
const date2 = new Date('2023-06-15');

const timeDifference = date2 - date1;

console.log(`The timespan between the two dates is: ${timeDifference} milliseconds.`);
// Output: "The timespan between the two dates is: 1209600000 milliseconds."

JavaScript date minus days

Subtracting one day from a date is a common operation when working with date manipulations. To subtract 1 day from a date, you can use the setDate() method of the Date object.

Here’s an example of subtract one day from date:

const date = new Date();
date.setDate(date.getDate() - 1);

console.log(`The date one day ago was: ${date.toLocaleDateString()}`);
// Output: "The date one day ago was: 6/16/2023"

 This method logs a message to the console displaying the updated date in a localized string format using JavaScript toLocaleDateString(). It is possible to customize a date display format like YYYY-MM-DD in JavaScript.

JavaScript date subtract 7 days from current date

To subtract 7 days from a date, you can use the setDate() method of the Date object. Here’s an example JavaScript date subtract days:

const date = new Date();
date.setDate(date.getDate() - 7);

console.log(`The date 7 days ago was: ${date.toLocaleDateString()}`);
// Output: "The date 7 days ago was: 6/10/2023"

How to subtract 30 days from Date in JavaScript?

To subtract 30 days from a date, you can use the setDate() method of the Date object. Here’s an example code snippet:

const date = new Date();
date.setDate(date.getDate() - 30);
console.log(`The date 30 days ago was: ${date.toLocaleDateString()}`);
// Output: "The date 30 days ago was: 5/18/2023"

Subtract 6 months from date

Subtracting a specific duration, like 6 months, from a date can be helpful in various scenarios. To subtract months from a date, you can use the setMonth() method of the Date object. Here’s an example code snippet:

const date = new Date();
date.setMonth(date.getMonth() - 6);

console.log(`The date 6 months ago was: ${date.toLocaleDateString()}`);
// Output: "The date 6 months ago was: 12/17/2022"

Subtract date from now (current date)

Subtracting a specific date from the current time can help determine the elapsed time. To subtract a date from the current time, you can subtract the date in milliseconds from the current timestamp. Here’s an example code snippet:

const date = new Date('2023-06-01');
const now = new Date();

const timeDifference = now - date;
console.log(`The difference between the date and now is: ${timeDifference} milliseconds.`);
// Output: "TThe difference between the date and now is: 1427395855 milliseconds."

Subtract dates and times

Subtracting date and time values provides more precise calculations for specific scenarios. To subtract date and time values, you can subtract the timestamps of the respective dates. Here’s an example code snippet:

const date1 = new Date('2023-06-01 10:00:00');
const date2 = new Date('2023-06-01 09:30:00');

const timeDifference = date1 - date2;
console.log(`The difference in date and time is: ${timeDifference} milliseconds.`);
// Output: "The difference in date and time is: 1800000 milliseconds."

How to subtract time from date in JavaScript?

Subtracting time values from a specific date can help track durations. To subtract time values from a date, you can utilize the set* methods of the Date object. Here’s an example code snippet:

const date = new Date();

date.setHours(date.getHours() - 2);
date.setMinutes(date.getMinutes() - 30);

console.log(`The date 2 hours and 30 minutes ago was: ${date}`);
// Output: "The date 2 hours and 30 minutes ago was: Sat Jun 17 2023 15:30:59 GMT+0530 (India Standard Time)"

How to subtract year JavaScript date?

To subtract a year from a date, you can use the setFullYear() method of the Date object. Here’s an example code snippet:

const date = new Date();
date.setFullYear(date.getFullYear() - 1);

console.log(`The date one year ago was: ${date.toLocaleDateString()}`);
// Output: "The date one year ago was: 6/17/2022"

In this post, explored different methods and use cases for subtracting dates, you can add dates and days as well to the JavaScript date object.

Frequently asked questions

To subtract dates in JavaScript and get the number of days between them, you can use the following code:
const date1 = new Date('2023-10-01');
const date2 = new Date('2023-10-15');
const timeDiff = date2 - date1;
const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
console.log(daysDiff); 
// Output: 14
You can calculate the difference in minutes between two dates like this:
const date1 = new Date('2023-10-01 12:30:00');
const date2 = new Date('2023-10-01 14:45:30');
const timeDiff = (date2 - date1) / (1000 * 60); // Divide by 60000 to convert to minutes
console.log(timeDiff); 
// Output: 135.5 minutes
You can divide by 1000 to get difference in seconds.
const timeDiff = (date2 - date1) / 1000;
You can calculate a person’s age from their birthdate using the following code: 
const birthdate = new Date('1990-05-15');
const today = new Date();
let age = today.getFullYear() - birthdate.getFullYear();
if (today.getMonth() < birthdate.getMonth() || 
    (today.getMonth() === birthdate.getMonth() && today.getDate() < birthdate.getDate())) {
    age--; // Adjust age if the birthday hasn't occurred yet this year
}
console.log(age); 
// Output: 33 (assuming current date is 2023-10-16)
You can subtract 1 day from the current date.
const date = new Date();
date.setDate(date.getDate() - 1);

console.log(date.toLocaleDateString());
// Output: "2/19/2024"

Conclusion

In conclusion, you’ve learned various techniques and code examples to subtract dates, calculate time differences, and perform date manipulations.

By mastering these skills, you’ll be equipped to handle a wide range of date-related scenarios in your JavaScript projects. So go ahead, experiment with different date subtraction methods, and unleash the power of JavaScript’s date manipulation capabilities.

Scroll to Top