Initialize A Date To Midnight Using JavaScript

Initialize a Date to Midnight using JavaScript

Sometimes, the service may not be available at the end of the day, and you have a request to initiate a day to midnight. This article will show you how to initialize a date to midnight using JavaScript. Let’s check it out!

How to initialize a date to midnight using JavaScript

In this article, we will share three methods to initialize a date to midnight using JavaScript:

  • Use the setHours() method
  • Use the setUTCHours() method
  • Use the startOf() method of the moment.js library

Use the setHours() method

The setHours() method sets the time in a day. This method sets the time relative to the time spent using the client’s application.

Syntax:

setHours(hour, min, sec, milisec)

Parameters:

  • hour: 0 to 23 (The hour of the day), -1 (The last hour of the previous day), 24 (The first hour of the next day).
  • min: 0 to 59 (The minute of the hour), -1 (The last minute of the previous hour), 60 (The first minute of the next hour).
  • sec: 0 to 59 (The second of the minute), -1 (The last second of the previous minute), 60 (The first second of the next minute).
  • milisec: 0 to 999 (The millisecond of the second), -1 (The last millisecond of the previous second), 1000 (The first millisecond of the next second).

First, we use the Date() constructor to create a new date object with the current date and time. Then we call the setHours() method on the newly created date object.

Example:

const day = new Date();

console.log(day);

// Midnight of today
const today = new Date();
today.setHours(0, 0, 0, 0);

console.log(today);

// Midnight of tomorrow
const tomorrow = new Date();
tomorrow.setHours(24, 0, 0, 0);

console.log(tomorrow);

// Midnight of of a specific date. Example: '2022-10-10'
const specificDay = new Date('2022-10-10');
specificDay.setHours(0, 0, 0, 0);

console.log(specificDay);

Output:

2022-10-08T01:22:10.679Z
2022-10-08T00:00:00.000Z
2022-10-09T00:00:00.000Z
2022-10-10T00:00:00.000Z

In case it is necessary to initialize a date to midnight for yesterday or a day before (after) the current day n days. We will do the following:

Firstly, call the getDate() method on the created date object. The getDate() method will return the day of the month. At this point, we subtract n values for the date we have just received.

Next, we use the setDate() method with the date we have just received.

Example:

const day = new Date();

console.log(day);

// Midnight of of any date n days before the current time. Example: n = 5
const dayBefore = new Date();
dayBefore.setDate(dayBefore.getDate() - 5);
dayBefore.setHours(0, 0, 0, 0);

console.log(dayBefore);

// Midnight of of any date n days after the current time. Example: n = 5
const dayAfter = new Date();
dayAfter.setDate(dayAfter.getDate() + 5);
dayAfter.setHours(0, 0, 0, 0);

console.log(dayAfter);

Output:

2022-10-08T01:24:15.917Z
2022-10-03T00:00:00.000Z
2022-10-13T00:00:00.000Z

Use the setUTCHours() method

According to UTC, the setUTCHours() method is used to set the time in a day. This method sets the time relative to the time spent using the client’s application.

This method sets the time in Coordinated Universal Time (UTC).

Syntax:

setUTCHours(hour, min, sec, millisec)

Parameters: (Same as setHours() method).

The setUTCHours() method is used the same way as the setHours() method.

Example:

const day = new Date();

console.log(day);

// Midnight of today
const today = new Date();
today.setUTCHours(0, 0, 0, 0);

console.log(today);

// Midnight of tomorrow
const tomorrow = new Date();
tomorrow.setUTCHours(24, 0, 0, 0);

console.log(tomorrow);

// Midnight of of a specific date. Example: '2022-10-10'
const specificDay = new Date('2022-10-10');
specificDay.setUTCHours(0, 0, 0, 0);

console.log(specificDay);

Output:

2022-10-08T01:26:16.326Z
2022-10-08T00:00:00.000Z
2022-10-09T00:00:00.000Z
2022-10-10T00:00:00.000Z

Use the startOf() method of the moment.js library.

The moment.js library allows us to set a date to midnight using the startOf() method with the 'day' attribute. This will set the date-time of the moment object to midnight of the day on which the moment is enabled.

Example:

import moment from "moment";

const day = moment().startOf('day');
console.log(day)

Output:

Moment<2022-10-08T00:00:00.000Z>

Summary

This article has shown how to initialize a date to midnight using JavaScript. I hope the information in this article will be helpful to you. Thanks for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *