Kodeclik Blog


How to get yesterday’s date (or tomorrow’s date) in Javascript

We will learn how to compute and modify dates in your Javascript program, specifically how to programmatically obtain yesterday’s and tomorrow’s dates To understand how to do this, we will write and embed our Javascript code inside a HTML page like so:
In the above HTML page you see the basic elements of the page like the head element containing the title ("Computing dates in Javascript") and a body with an empty script tag. Any HTML markup goes inside the body tags and any Javascript code goes inside the script tags.

Find today’s date

Let us first try to write a program to print and reason about today’s date. We can use the “new Date()” constructor to obtain a Date object containing the current date and time. Here is a simple Javascript program that initializes a Date object and then writes it out into your HTML page:
The output will be (depending on the day you read this blogpost and/or run this program):
Note that the Date object contains the time as well as the date.
If you would like to extract the day, month,and year specifically, we can use methods like getDate, getMonth, and getYear:
The output is:
Wow - what happened? The Day is correct but the Month and Year are not quite right. The reason the getMonth() output appears incorrect is that it counts months from 0 so the “6” actually denotes the 7th month, viz. July. The getYear() output appears incorrect because it counts years from the year 1900. Thus, 122 is really 1900+122 = 2022. In general, getYear() is deprecated. Instead we must use getFullYear(). Here’s the updated code to account for all of the above:
The output is now:
The year is fixed now but the month is still not quite right. This is because the getMonth()+1 essentially does string concatenation and hence the output of “6” is concatenated with “1” to obtain 61. To fix this, we need to do a simple update to our code as follows:
The output is:
Here, getMonth() returns an integer so that the “+1” is interpreted as integer addition whereas in the earlier code it was in the larger context of string concatenation. This is why the output is exactly what we need, i.e., 19th July 2022.

Find yesterday’s date

Now we need to update our code to find yesterday’s date. One approach is to subtract one day from the Date, see if it crosses a month threshold and in that case, update the month, and so on to see if we will need to update the year. This can make for some cumbersome code. An easier approach is to convert the date to seconds, then subtract the seconds corresponding to a year, and then reconstructing the date.
In the below code, we will use the getTime() method which returns the number of milliseconds since January 1, 1970 00:00:00.
The output is:
Now let us find yesterday’s date by subtracting one day’s worth of seconds. Then we use the Date() constructor again to obtain the date in traditional date format. Here is a program to accomplish this objective:
As we can see once we obtain today’s time stamp we subtract 24*60*60*1000 worth of milliseconds. The output is:

Find tomorrow’s date

Now that we have gone through the above exercise, finding tomorrow’s date is simple. We just need to update the calculations accordingly:
The output is:
We have learnt how to work with dates in Javascript, including finding yesterday’s date and tomorrow’s date programmatically. How will you make use of these capabilities?
If you liked this blogpost, checkout our blogpost on finding your current time zone programmatically in Javascript!
Interested in learning more Javascript? Learn how to loop through two Javascript arrays in coordination and how to make a Javascript function return multiple values!
Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2022. All rights reserved.