Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 29, 2022 01:19 pm GMT

Get Yesterday Date in JavaScript

Learn to remove one day-to-date in JavaScript using the native date object.

How to Get Yesterday Date in JavaScript?

The best way to get yesterdays date in JavaScript is by using the Date object. On an existing Date, you can use the getDate function to get the day (between 1 and 31), subtract 1 to this number, then use setDate to update the date. Here's a short example: date.setDate(date.getDate() - 1).

Using the JavaScript Date Object

In JavaScript, youll often manipulate the Date object to do diverse operations (e.g., subtract days from a date, format a date, etc.).

If you want to get yesterdays date in JavaScript, you can use a combination of these two Date functions:

  1. yourDate.getDate(): get the day of the month (you'll get a number between 1 and 31)

  2. yourDate.setDate(): update the day of the date to the number passed as a parameter

In practice, lets say you have this date: 15/11/2022. The getDate() function will return 15. Then you can subtract one day to this number 15 - 1 = 14. Finally, use setDate() to update the day of the month for your current date object.

Heres a commented example:

// Create a dateconst todayDate = new Date()// Before subtracting 1 dayconsole.log(todayDate.toString())// Output: "Tue Nov 15 2022 13:37:12 GMT+0100 (Central European Standard Time)"// Subtract one day to the current datetodayDate.setDate(todayDate.getDate() - 1)// After removing 1 dayconsole.log(todayDate.toString())// Output: "Mon Nov 14 2022 13:37:12 GMT+0100 (Central European Standard Time)"

Now that you know how to subtract days from a date, you can learn how to add one day to date in JavaScript.

Bonus: Create a Function to Get Yesterday Date

Now you know how to remove days to a date, lets make your code fancy!

One way to do that is to create a function that takes a date as a parameter, subtracts one day, and returns it.

Following what we did in the last part, heres how:

// Create a function to make the logic genericconst removeOneDayToDate = (date) => {  date.setDate(date.getDate() - 1)  return date}// Get the current dateconst date = new Date()// Before removing 1 dayconsole.log(date.toString())// Output: "Tue Nov 15 2022 13:37:12 GMT+0100 (Central European Standard Time)"// Call `removeOneDayToDate` with the current date// and assign the result to a new variable called `yesterdayDate`const yesterdayDate = removeOneDayToDate(date);// After subtracting 1 dayconsole.log(yesterdayDate.toString());// Output: "Mon Nov 14 2022 13:37:12 GMT+0100 (Central European Standard Time)"

Thanks for reading. Lets connect!

I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more.


Original Link: https://dev.to/gaelgthomas/get-yesterday-date-in-javascript-1841

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To