Get date of current day, next day, previous day and several days in Javascript

Today we will learn about how to get date of current day, next day, previous day and several days in Javascript.

The first, for each case we'll create one function to get and convert date, consider the functions below:

1. Date of previous day:

const previousDate = () => {
  const date = new Date();
  return new Date(date.setDate(date.getDate() - 1)).toLocaleDateString();
};


2. Date of current day:

 const currentDate = () => {
    const date = new Date();
    return date.toLocaleDateString();
 };


3. Date of next day:

const nextDate = () => {
  const date = new Date();
  return new Date(date.setDate(date.getDate() + 1)).toLocaleDateString();
};


4. Date of several days:

const severalDate = (several, next) => {
 const date = new Date();
 const dateGet = next ? date.getDate() + several : date.getDate() - several;
 return new Date(date.setDate(dateGet)).toLocaleDateString();
};


Usage:

previousDate() // previous day
currentDate() // current day
nextDate() // next day
severalDate(5, true) // next 5 days
severalDate(5, false) // previous 5 days


If today is 4/20/2020, we will have result like this:



get datejavascriptset date

© 2019 4codev

Created with love by Sil.