[javascript] Javascript code for showing yesterday's date and todays date

How to show yesterday's date in my textbox the yesterday's date and at the same time, the today's date in ?

I have this home.php where I show the date yesterday(user cannot modify this-readonly) and the date today(user MUST input the date today). And when tomorrow comes and the user visits the home .php s/he will saw the date inputted yesterday, and will input the date again for romorrow's.

E.G: Day1 (march 30, 2011) Yesterday's date: March 29, 2011. (Not editable textbox) Enter date today: (I'll type..) March 30, 2011.

Day 2 (march 31, 2011) Yesterday's date: March 30, 2011. (Not editable textbox) Automatically, this will appear upon hitting the home.php Enter date today: (I'll type..) March 31, 2011.

and so on..

I need a validation that wont accept wrong date format and the format must be: 01-Mar-11 How to do this? :(

This question is related to javascript date

The answer is


Yesterday Date can be calculated as:-

let now = new Date();
    var defaultDate = now - 1000 * 60 * 60 * 24 * 1;
    defaultDate = new Date(defaultDate);

One liner:

var yesterday = new Date(Date.now() - 864e5); // 864e5 == 86400000 == 24*60*60*1000

Yesterday's date can be calculated as,

var prev_date = new Date();
prev_date.setDate(prev_date.getDate() - 1);

Get yesterday date in javascript

You have to run code and check it output

_x000D_
_x000D_
    var today = new Date();_x000D_
    var yesterday = new Date(today);_x000D_
    _x000D_
    yesterday.setDate(today.getDate() - 1);_x000D_
    console.log("Original Date : ",yesterday);_x000D_
_x000D_
    const monthNames = [_x000D_
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"_x000D_
];_x000D_
    var month = today.getMonth() + 1_x000D_
    yesterday = yesterday.getDate() + ' ' + monthNames[month] + ' ' + yesterday.getFullYear()_x000D_
   _x000D_
    console.log("Modify Date : ",yesterday);
_x000D_
_x000D_
_x000D_