javascript today minus 1 day

javascript today minus 1 day
// Get the current date 
var today = new Date(); 

// Subtract 1 day
today.setDate(today.getDate() - 1);

// Log the new date
console.log(today); 

This code will get the current date (today) and subtract 1 day from it. It does this by creating a new Date object (which will always return the current date) and then using the setDate() method to subtract 1 day from it. The Date object also has a getDate() method which can be used to get the day of the month that the Date object represents. It then logs the new date to the console, which will display the date from 1 day ago. It is important to note that when you subtract 1 day from the Date object, it will always return the same time of day (midnight) so if you subtract 1 day from a Date object that is not midnight, it will return the same day at midnight. Additionally, if you want to subtract more than 1 day from the Date object, you can just increase the number passed to the setDate() method. For example, if you wanted to subtract 2 days, you could use today.setDate(today.getDate() - 2);

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe