How to Get the Beginning and End of the Day with MomentJS?



Sometimes, we need to find the beginning and end of the day using MomentJS. This functionality has different real-life applications, such as filtering data within a day's range, generating daily reports, and scheduling tasks at specific times. In this article, we will discuss how to find the beginning and end of the day using MomentJS.

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 Get the Beginning and End of the Day

Below are different approaches to getting the beginning and end of the day with MomentJS.

Using startOf() and endOf() Methods

This is a simple approach to finding the beginning and ending of the day. This is an inbuilt function provided by MomentJS. where the day is the current day or any specific day we will pass to find the beginning and end of the day.

Example

// Import Moment.js
const moment = require('moment');

// Get the beginning of the current day
const startOfDay = moment().startOf('day');
console.log("Start of Day:", startOfDay.format("YYYY-MM-DD HH:mm:ss"));

// Get the end of the current day
const endOfDay = moment().endOf('day');
console.log("End of Day:", endOfDay.format("YYYY-MM-DD HH:mm:ss"));

Output

Start of Day: 2024-12-26 00:00:00
End of Day: 2024-12-26 23:59:59

Using Moment.js with a Specific Date

If we want to find the starting and ending of any specific day, we can pass the specific date rather than the current day. We can pass the date as a parameter to Moment.js.

Example

// Specify a custom date
const specificDate = moment("2024-12-25");

// Get the beginning of the specific day
const startOfSpecificDay = specificDate.startOf('day');
console.log("Start of Specific Day:", startOfSpecificDay.format("YYYY-MM-DD HH:mm:ss"));

// Get the end of the specific day
const endOfSpecificDay = specificDate.endOf('day');
console.log("End of Specific Day:", endOfSpecificDay.format("YYYY-MM-DD HH:mm:ss"));

Ouptut

Start of Day: 2024-12-26 00:00:00
End of Day: 2024-12-26 23:59:59

Using UTC Dates

Moment.JS supports Coordinated Universal Time (UTC). If we are working with multiple time zones, we can use UTC to ensure consistency.

Example

// Get the beginning of the current day in UTC
const startOfDayUTC = moment().utc().startOf('day');
console.log("Start of Day (UTC):", startOfDayUTC.format("YYYY-MM-DD HH:mm:ss"));

// Get the end of the current day in UTC
const endOfDayUTC = moment().utc().endOf('day');
console.log("End of Day (UTC):", endOfDayUTC.format("YYYY-MM-DD HH:mm:ss"));

Output

Start of Day (UTC): 2024-12-25 00:00:00
End of Day (UTC): 2024-12-25 23:59:59
Updated on: 2024-12-30T09:14:01+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements