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
Selected Reading
JavaScript program to decrement a date by 1 day
JavaScript provides several methods to manipulate dates. To decrement a date by 1 day, we can use the setDate() and getDate() methods together.
Syntax
date.setDate(date.getDate() - 1);
Parameters
- date.getDate() - Returns the current day of the month (1-31)
- date.setDate() - Sets the day of the month for the date object
Example: Basic Date Decrement
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Decrement Date by 1 Day</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
padding: 20px;
}
.result, .sample {
font-size: 18px;
font-weight: 500;
color: rebeccapurple;
margin: 10px 0;
}
.result {
color: green;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>JavaScript program to decrement a date by 1 day</h1>
<div class="sample"></div>
<div class="result"></div>
<button class="Btn">Decrement Date</button>
<h3>Click the button to decrement the date by one day</h3>
<script>
let resEle = document.querySelector(".result");
let sampleEle = document.querySelector(".sample");
let date = new Date();
sampleEle.innerHTML = "Original Date: " + date.toDateString();
document.querySelector(".Btn").addEventListener("click", () => {
date.setDate(date.getDate() - 1);
resEle.innerHTML = "Decremented Date: " + date.toDateString();
});
</script>
</body>
</html>
Example: Console-based Date Decrement
// Create a new date object
let currentDate = new Date();
console.log("Original date:", currentDate.toDateString());
// Decrement by 1 day
currentDate.setDate(currentDate.getDate() - 1);
console.log("Decremented date:", currentDate.toDateString());
// Example with specific date
let specificDate = new Date("2024-01-01");
console.log("Before decrement:", specificDate.toDateString());
specificDate.setDate(specificDate.getDate() - 1);
console.log("After decrement:", specificDate.toDateString());
Original date: Mon Jan 15 2024 Decremented date: Sun Jan 14 2024 Before decrement: Mon Jan 01 2024 After decrement: Sun Dec 31 2023
How It Works
The setDate() method automatically handles month and year transitions. When you decrement the first day of a month, JavaScript automatically adjusts to the previous month's last day. This works seamlessly across month and year boundaries.
Key Points
-
getDate()returns the day of the month (1-31) -
setDate()automatically handles month/year rollovers - The date object is modified in place
- Works correctly with leap years and different month lengths
Conclusion
Using setDate(date.getDate() - 1) is the most reliable way to decrement a date by one day in JavaScript. The method automatically handles month and year boundaries, making date manipulation simple and error-free.
Advertisements
