w3resource

JavaScript: Find out which day was in yesterday

JavaScript Datetime: Exercise-10 with Solution

Write a JavaScript function to calculate 'yesterday's day.

Test Data:
console.log(yesterday('Nov 15, 2014'));
console.log(yesterday('Nov 16, 2015'));
console.log(yesterday('Nov 17, 2016'));
Output :
"Fri Nov 14 2014 00:00:00 GMT+0530 (India Standard Time)"
"Sun Nov 15 2015 00:00:00 GMT+0530 (India Standard Time)"
"Wed Nov 16 2016 00:00:00 GMT+0530 (India Standard Time)"

Sample Solution:

JavaScript Code:

// Define a JavaScript function called yesterday with parameter date1
var yesterday =  function(date1){
    // Create a new Date object by parsing the provided date string
    var dt = new Date(date1);
    // Set the date of the Date object to the previous day
    dt.setDate(dt.getDate() - 1);
    // Return the previous day as a string representation
    return new Date(dt).toString();
}

// Output the previous day for the date 'Nov 15, 2014'
console.log(yesterday('Nov 15, 2014'));
// Output the previous day for the date 'Nov 16, 2015'
console.log(yesterday('Nov 16, 2015'));
// Output the previous day for the date 'Nov 17, 2016'
console.log(yesterday('Nov 17, 2016'));

Output:

Fri Nov 14 2014 00:00:00 GMT+0530 (India Standard Time)
Sun Nov 15 2015 00:00:00 GMT+0530 (India Standard Time)
Wed Nov 16 2016 00:00:00 GMT+0530 (India Standard Time)

Explanation:

In the exercise above,

  • The code defines a JavaScript function named "yesterday()" with one parameter 'date1', representing a date string.
  • Inside the function:
    • It creates a new Date object 'dt' by parsing the provided date string 'date1'.
    • It then adjusts the date of the 'dt' object to the previous day by subtracting 1 from the day component using dt.setDate(dt.getDate() - 1).
    • This effectively modifies the date to represent the previous day.
  • It creates a new Date object from the modified 'dt' object and converts it to a string representation using .toString().
  • The function returns the string representation of the previous day.
  • The code then demonstrates the "yesterday()" function by calling it with three different date strings:
    • 'Nov 15, 2014'
    • 'Nov 16, 2015'
    • 'Nov 17, 2016'

Flowchart:

Flowchart: JavaScript- Find out the last day of a month

Live Demo:

See the Pen JavaScript - Find out which day was in yesterday-date-ex- 10 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to get the last day of a month.
Next: Write a JavaScript function to get the maximum date from an array of dates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript-exercises/javascript-date-exercise-10.php