How to set 00:00:00 using MomentJS?



Sometimes while working with dates and times, we need to manipulate or normalize them to a specific time. One such common need is to reset a date's time to the start of the day, i.e. 00:00:00 midnight. In this article, we are going to learn different approaches to set the time to 00:00:00 using Moment.js.

Prerequisite

  • Moment.js: Moment.js is a popular JavaScript library used to deal with dates. This library is used to modify, parse, validate, manipulate, and display dates and times in JavaScript.

Install Moment.js

We can install the Moment.js library in the project by adding it via CDN or npm using the following command:

npm install moment

Approaches to set 00:00:00 using Moment.js

Using the startOf() Method

In this approach, we use startof() method in Moment.js to set the date to the beginning of a specific unit of time such as "day," "month," or "year." If we want to set the time to 00:00:00 of the current day, we use "day". In this method, we use moment.startOf to set the time to 00:00:00 midnight. This inbuilt function is provided by the moment.js library.

Example

// Get the Current date and time
let date = moment();

// Set time to 00:00:00 midnight
let midnight = date.startOf('day');

// Output the result
console.log("Midnight Time: ", midnight.format('YYYY-MM-DD HH:mm:ss'));

Output

Midnight Time: 2024-12-20 00:00:00

Using the set() Method

We can use the set() methodM to directly set the values of time components such as hour, minute, second, and millisecond. To set the time 00:00:00 we would set all the values to 0(zero). Using this method we set each parameter of time i.e. hour, minute, and second to zero using the set method. This inbuilt method is provided by the Moment.js library. This method is mainly used for changing multiple time components.

Example

const moment = require('moment');

// Current date and time
let date = moment();

// Set hour, minute, second, and millisecond to 0
date.set({ hour: 0, minute: 0, second: 0, millisecond: 0 });

// Output the result
console.log("Midnight Time: ", date.format('YYYY-MM-DD HH:mm:ss'));

Output

Midnight Time: 2024-12-17 00:00:00

Using the hour(), minute(), and second() Methods

We can manually set the time as 00:00:00 midnight by manually setting the time components using the .hour(), .minute(), .second(), and .millisecond() methods. This approach is very specific and gives us control over setting the time components we want. This is the most useful method as we can set any time we want according to our needs. It offers flexibility to customize time settings beyond setting everything to zero.

Example

// Load moment.js library
const moment = require('moment');

// Current date and time
let date = moment();

// Set hour, minute, second, and millisecond manually
date.hour(0).minute(0).second(0).millisecond(0);

// Output the result
console.log("Midnight Time: ", date.format('YYYY-MM-DD HH:mm:ss'));

Output

Midnight Time: 2024-12-17 00:00:00

Conclusion

We have discussed three methods above to set the time to 00:00:00 midnight using the Moment.js library in JavaScript. This time is very useful for measuring time and making calculations from it.

Updated on: 2024-12-23T11:03:36+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements