Introduction
In this lab, we will explore the basics of programming in JavaScript. We will cover topics such as variables, data types, operators, and control structures. By the end of the lab, you will have a solid foundation in JavaScript programming and be able to write simple scripts and functions.
Getting Yesterday's Date in yyyy-mm-dd Format
To get yesterday's date in yyyy-mm-dd format, follow these steps:
- Open the Terminal/SSH and type
nodeto start practicing coding. - Use the
Dateconstructor to get the current date. - Decrement the date by one using
Date.prototype.getDate(). - Set the decremented date using
Date.prototype.setDate(). - Use
Date.prototype.toISOString()to return a string inyyyy-mm-ddformat. - Call the function
yesterday()to get yesterday's date.
const yesterday = () => {
let d = new Date();
d.setDate(d.getDate() - 1);
return d.toISOString().split("T")[0];
};
yesterday(); // returns "2018-10-17" (if current date is 2018-10-18)
By following these steps, you will be able to retrieve yesterday's date in a clear and concise manner.
Summary
Congratulations! You have completed the Date of Yesterday lab. You can practice more labs in LabEx to improve your skills.