Home Featured Mastering Time- How to Retrieve Yesterday’s Date in JavaScript

Mastering Time- How to Retrieve Yesterday’s Date in JavaScript

by liuqiyue

How to Get Yesterday Date in JavaScript

JavaScript is a versatile programming language widely used for web development. One common task in JavaScript is to work with dates and times. Whether you are building a calendar, a countdown timer, or any application that requires date manipulation, knowing how to get yesterday’s date is essential. In this article, we will explore various methods to retrieve yesterday’s date in JavaScript.

Method 1: Using the Date Object

The simplest way to get yesterday’s date in JavaScript is by using the Date object. The Date object represents a specific instant in time, with millisecond precision. To get yesterday’s date, you can subtract one day from the current date.

“`javascript
// Get the current date
const currentDate = new Date();

// Subtract one day from the current date
const yesterday = new Date(currentDate.setDate(currentDate.getDate() – 1));

console.log(yesterday.toISOString()); // Output: “2023-04-14T00:00:00.000Z”
“`

In the above code, we first create a new Date object with the current date and time. Then, we use the `setDate()` method to subtract one day from the current date. Finally, we convert the resulting date to an ISO string format using the `toISOString()` method.

Method 2: Using the Moment.js Library

If you prefer using a library for date manipulation, Moment.js is a popular choice. It provides a wide range of functions to work with dates and times. To get yesterday’s date using Moment.js, you can follow these steps:

“`javascript
// Import the moment library
const moment = require(‘moment’);

// Get yesterday’s date
const yesterday = moment().subtract(1, ‘days’);

console.log(yesterday.format()); // Output: “2023-04-13”
“`

In this code, we first import the Moment.js library. Then, we use the `subtract()` method to subtract one day from the current date. Finally, we format the resulting date using the `format()` method.

Method 3: Using the Luxon.js Library

Another popular library for date manipulation is Luxon.js. It is a modern JavaScript library for parsing, validating, manipulating, and formatting dates. To get yesterday’s date using Luxon.js, you can use the following code:

“`javascript
// Import the Luxon library
const { DateTime } = require(‘luxon’);

// Get yesterday’s date
const yesterday = DateTime.now().minus({ days: 1 });

console.log(yesterday.toISODate()); // Output: “2023-04-13”
“`

In this code, we first import the Luxon library and its `DateTime` module. Then, we use the `minus()` method to subtract one day from the current date. Finally, we convert the resulting date to an ISO date string using the `toISODate()` method.

Conclusion

In this article, we discussed three methods to get yesterday’s date in JavaScript. By using the Date object, Moment.js, or Luxon.js, you can easily retrieve yesterday’s date and manipulate it as needed in your application. Whether you are working on a simple script or a complex web application, these methods will help you handle date-related tasks efficiently.

Related Posts