Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to subtract days from a date in JavaScript?
In JavaScript, you can subtract days from a date using built-in Date methods. The most common approaches are using setTime() with getTime() for millisecond-based calculation, or setDate() with getDate() for simpler day-based arithmetic.
Following are the methods we can use to subtract days from a date in JavaScript:
-
Using the
setTime()andgetTime()methods -
Using the
setDate()andgetDate()methods
Using setTime() and getTime() Methods
The setTime() method sets a date by milliseconds since January 1, 1970, while getTime() returns the current date as milliseconds. This approach is precise and handles month/year boundaries automatically.
Syntax
date.setTime(milliseconds); date.getTime();
Example
<!DOCTYPE html>
<html>
<body>
<h2>Subtract days from a date in JavaScript</h2>
<p id="result1"></p>
<p>Click below button to subtract 10 days from today's date</p>
<button onclick="subtractDays()">Subtract 10 Days</button>
<p id="result2"></p>
<script>
var today = new Date();
var result1 = document.getElementById("result1");
var result2 = document.getElementById("result2");
result1.innerHTML = "<b>Today's Date: </b>" + today.toDateString();
function subtractDays() {
var newDate = new Date();
var millisecondsToSubtract = 10 * 24 * 60 * 60 * 1000; // 10 days in milliseconds
newDate.setTime(today.getTime() - millisecondsToSubtract);
result2.innerHTML = "<b>Date after subtracting 10 days: </b>" + newDate.toDateString();
}
</script>
</body>
</html>
In this example, we calculate 10 days in milliseconds (10 × 24 × 60 × 60 × 1000) and subtract it from the current time.
Using setDate() and getDate() Methods
The setDate() method sets the day of the month (1-31), while getDate() returns the current day. This method automatically handles month and year rollovers when subtracting days.
Syntax
date.setDate(dayOfMonth); date.getDate();
Example
<!DOCTYPE html>
<html>
<body>
<h2>Subtract days from a date in JavaScript</h2>
<p id="result1"></p>
<p>Click the below button to subtract 12 days from today's date</p>
<button onclick="subtractDays()">Subtract 12 Days</button>
<p id="result2"></p>
<script>
var today = new Date();
var result1 = document.getElementById("result1");
var result2 = document.getElementById("result2");
result1.innerHTML = "<b>Today's Date: </b>" + today.toDateString();
function subtractDays() {
var newDate = new Date(today);
var currentDay = newDate.getDate();
newDate.setDate(currentDay - 12);
result2.innerHTML = "<b>Date after subtracting 12 days: </b>" + newDate.toDateString();
}
</script>
</body>
</html>
This approach directly subtracts days from the current day of the month. JavaScript automatically adjusts the month and year if the result goes into the previous month.
Comparison
| Method | Precision | Complexity | Use Case |
|---|---|---|---|
setTime()/getTime() |
Millisecond-precise | Medium | Time-sensitive calculations |
setDate()/getDate() |
Day-precise | Simple | Basic date arithmetic |
Key Points
- Both methods automatically handle month and year boundaries
-
setDate()approach is simpler for basic day subtraction -
setTime()approach is more precise for time-sensitive operations - Always create a new Date object to avoid modifying the original date
Conclusion
Use setDate() with getDate() for simple day subtraction, or setTime() with getTime() for more precise millisecond-based calculations. Both methods handle date boundaries automatically.
