Blog / Javascript

How to get yesterday's date in JavaScript

How to get yesterday's date in JavaScript

Find out how to quickly and easily calculate and get yesterday's date in JavaScript without needing to use any libraries.

Will MaygerWill Mayger
October 25, 2021
Article

In this post we will be covering how to get yesterday’s date in JavaScript with as little technical jargon as possible so you will have everything you need right here without having to look any further. It is actually very quick and easy to do and you won’t need to import any libraries!

How to get yesterday’s date in JavaScript

To get yesterday’s date in JavaScript all we need to do is make use of the Date Object’s methods Date.prototype.getDate, and Date.prototype.setDate in combination with the subtraction operator to subtract the date by 1.

In other words we first get today’s date, subtract one day from it, and then save the result in order to get yesterday’s date.

Here is an example of how to get yesterday’s date in JavaScript:

const today = new Date();
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);

console.log(yesterday); // Sun Oct 24 2021 22:22:07 GMT+0100 (British Summer Time)
how to get yesterday's date in JavaScript

This will work for any day/date even if it is the first day of the month.

How to check if a date is today or yesterday in JavaScript

Now that we have the todays and yesterday’s date we can easily check if a given date is either today or yesterday.

Firstly we will need to reset the time so all the dates would be equal to the day and not the day and time.

After we reset the time we can then just perform a simple comparison to check if a date is today or yesterday.

const dateWithoutTime = someDate => new Date(someDate.toDateString());

const checkDateIsTodayOrYesterday = someDate => {
  const today = dateWithoutTime(new Date());
  const yesterday = dateWithoutTime(new Date());

  yesterday.setDate(today.getDate() - 1);

  const comparisonDateTime = dateWithoutTime(someDate).getTime();

  if (
    comparisonDateTime > today.getTime() ||
    comparisonDateTime < yesterday.getTime()
  ) {
    throw new Error("Invalid date - neither today or yesterday");
  }

  return comparisonDateTime === today.getTime() ? "today" : "yesterday";
};

const yesterdaysDate = new Date();
yesterdaysDate.setDate(yesterdaysDate.getDate() - 1);

console.log(checkDateIsTodayOrYesterday(new Date())); // 'today'
console.log(checkDateIsTodayOrYesterday(yesterdaysDate)); // 'yesterday'
console.log(checkDateIsTodayOrYesterday(new Date("01/02/2020"))); // Error "Invalid date - neither today or yesterday"
javascript check if date is today or yesterday

As you can see from the above example all we have done is package these principles into a couple of helper functions that can now check if a date that is provided is today or yesterday.

For more detail and information about comparing dates in JavaScript, have a read of my post about it here.

Formatting dates in JavaScript

When it comes to formatting dates in JavaScript the best option is to make use of a library such as date-fns to handle the formatting for you.

With date-fns, all you have to do is pass in your date and a string format and it will then do the rest of the work.

Here is a quick example of formatting yesterday’s date in JavaScript with date-fns to match yyyy-mm-dd or dd/mm/yyyy:

import { format } from "date-fns";

const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

console.log(format(new Date(), "yyyy-MM-dd")); // 2021-10-24
console.log(format(new Date(), "dd/MM/yyyy")); // 24/10/2021

Summary

There we have how to get yesterday’s date in JavaScript, if you want more like this be sure to check out some of my other posts!

I hope this post has helped, but before you go I highly recommend that you checkout Pluralsight if you are interested in improving your software engineering skills, such as React, JavaScript, or Typescript (and much more).

Not only does Pluralsight provide you with all the learning material you need but it also let's you learn in a fun, interactive way and gives you the ability to see your current progress and level in a certain skill via their Skill Assessment tool which can help you see what you need to learn next to keep improving!

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Some graphics used on this post were made using icons from flaticon.

Latest Posts

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Earn more and take your coding career to the next level.

Here you will find my personal recomendations to you, for full disclosure I earn a small commission from some of these links, but I only recommend what I trust and personally use.