How to calculate the yesterday's date in JavaScript ? - GeeksforGeeks

16 Feb, 2021
JavaScript | getDate()
JavaScript | setDate()
Problem Statement:In order to calculate yesterday’s date in JavaScript, we need to familiarize ourselves with 2 functions.
getDate()It is an inbuilt JavaScript function that returns the day of the month as a number (1-31).Syntax: dateObj.getDate() 
 dateObj.getDate() 
setDate()It is an inbuilt JavaScript function used to set the day of the month into a date object.Syntax: dateObj.setDate()Solution:JavaScript allows us to create a platform independent date instance that represents a single moment in time using Date constructor. An empty Date constructor creates a new date object that represents the current date and time. The Date constructor can also be specified to create a date object that represents a particular date and time. We use getDate() function to fetch the current date from the date object and subtract one day from it using the setDate() function which sets yesterday’s date onto the date object.Example 1:Here we will get the yesterday date, in this example we will show the output compare to today’s date.Syntax:var dateObj = new Date(); 
// empty Date constructor representing current time
dateobj; 
// Current Time => Wed Jun 12 2019 20:52:24 GMT+0530 (India Standard Time)
The following programs illustrate the solutionCode:<script>// JavaScript program to illustrate// calculation of yesterday's date    // create a date object using Date constructorvar dateObj = new Date();   // subtract one day from current time                          dateObj.setDate(dateObj.getDate() - 1);   alert(dateObj);   </script>                    Output:// Returns Yesterday's date and time
Example 2:Here we will get the yesterday date of pre-defined date which is Fri May 10 2019 16:30:00 GMT+0530 (India Standard Time).Syntax:var dateObj = new Date(2019, 04, 10, 16, 30, 00);
// specified Date constructor representing particular time   
dateObj; 
// Specific Time => Fri May 10 2019 16:30:00 GMT+0530 (India Standard Time)
Code:<script>// JavaScript program to illustrate// calculation of yesterday's date // of a specified date  // create a specified date object using Date constructorvar dateObj = new Date(2019, 04, 10, 16, 30, 00);  // subtract one day from specified time                          dateObj.setDate(dateObj.getDate() - 1);   alert(dateObj);   </script>                    Output:Supported browser:Google ChromeMozilla FirefoxInternet ExploreSafariOperaMy Personal Notes
arrow_drop_upSave
Syntax:
 dateObj.setDate()
Solution:
JavaScript allows us to create a platform independent date instance that represents a single moment in time using Date constructor. An empty Date constructor creates a new date object that represents the current date and time. The Date constructor can also be specified to create a date object that represents a particular date and time. We use getDate() function to fetch the current date from the date object and subtract one day from it using the setDate() function which sets yesterday’s date onto the date object.
Example 1:Here we will get the yesterday date, in this example we will show the output compare to today’s date.Syntax:
var dateObj = new Date(); 
// empty Date constructor representing current time
dateobj; 
// Current Time => Wed Jun 12 2019 20:52:24 GMT+0530 (India Standard Time)

The following programs illustrate the solution
Code:
<script>// JavaScript program to illustrate// calculation of yesterday's date    // create a date object using Date constructorvar dateObj = new Date();   // subtract one day from current time                          dateObj.setDate(dateObj.getDate() - 1);   alert(dateObj);   </script>                    
Output:
// Returns Yesterday's date and time

Example 2:Here we will get the yesterday date of pre-defined date which is Fri May 10 2019 16:30:00 GMT+0530 (India Standard Time).Syntax:
var dateObj = new Date(2019, 04, 10, 16, 30, 00);
// specified Date constructor representing particular time   
dateObj; 
// Specific Time => Fri May 10 2019 16:30:00 GMT+0530 (India Standard Time)

Code:
<script>// JavaScript program to illustrate// calculation of yesterday's date // of a specified date  // create a specified date object using Date constructorvar dateObj = new Date(2019, 04, 10, 16, 30, 00);  // subtract one day from specified time                          dateObj.setDate(dateObj.getDate() - 1);   alert(dateObj);   </script>                    
Output:
Google Chrome
Mozilla Firefox
Internet Explore
Safari
Opera
arorakashish0911
javascript-date
Picked
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
How to read a local text file using JavaScript?
Node.js | fs.writeFileSync() Method