
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to get yesterday’s date in JavaScript?
To get the yesterday date, first of all get the current date and subtract 1 from the current and use the function setDate(). The syntax is as follows −
yourCurrentDateVariableName.setDate(yourCurrentDateVariableName.getDate() - 1);
At first, get the current date −
var currentDate = new Date();
Now, get yesterday’s date with the following code −
Example
var currentDate = new Date(); console.log("The current date="+currentDate); var yesterdayDate = currentDate.setDate(currentDate.getDate()- 1); console.log("The yesterday date ="+new Date(yesterdayDate));
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo137.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo137.js The current date=Fri Jul 31 2020 18:57:17 GMT+0530 (India Standard Time) The yesterday date =Thu Jul 30 2020 18:57:17 GMT+0530 (India Standard Time)
- Related Articles
- How to select yesterday's date in MySQL?
- How to get today's date in Java8?
- How to subtract date from today's date in JavaScript?
- How to get yesterday records from timestamp in Android sqlite?
- How to manipulate JavaScript's Date object?
- How to get current date and time in JavaScript?
- How to get current date/time in seconds in JavaScript?
- How to get current date and time using JavaScript?
- JavaScript - Get Date Methods
- How to get Euler's constant value in JavaScript?
- How do I get the current date in JavaScript?
- How to convert a date object's content into json in JavaScript?
- How to get the certificate's start and expiry date using PowerShell?
- Get current date/time in seconds - JavaScript?
- Get all MySQL records from the previous day (yesterday)?

Advertisements