Date of Yesterday

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") subgraph Lab Skills javascript/variables -.-> lab-28250{{"`Date of Yesterday`"}} javascript/data_types -.-> lab-28250{{"`Date of Yesterday`"}} javascript/arith_ops -.-> lab-28250{{"`Date of Yesterday`"}} javascript/comp_ops -.-> lab-28250{{"`Date of Yesterday`"}} javascript/cond_stmts -.-> lab-28250{{"`Date of Yesterday`"}} end

Getting Yesterday's Date in yyyy-mm-dd Format

To get yesterday's date in yyyy-mm-dd format, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the Date constructor to get the current date.
  3. Decrement the date by one using Date.prototype.getDate().
  4. Set the decremented date using Date.prototype.setDate().
  5. Use Date.prototype.toISOString() to return a string in yyyy-mm-dd format.
  6. 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.

Other JavaScript Tutorials you may like