VOOZH about

URL: https://www.geeksforgeeks.org/javascript/check-whether-the-current-time-is-between-2-times-in-momentjs/

⇱ Check Whether the Current Time is Between 2 Times in Moment.JS? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check Whether the Current Time is Between 2 Times in Moment.JS?

Last Updated : 23 Jul, 2025

In Moment.js, checking whether the current time falls between two specified times involves comparing the current moment against the two given times. This allows you to determine if the present time is within a defined time range, making it useful for tasks like scheduling or time-based conditions.

Installing Moment.js in Node.js

We have first to install moment.js using the following command in the terminal:

npm install moment

Below are the different approaches to checking if a variable is a MomentJS object:

Using isBetween() Method

In this approach, we use Moment.js's isBetween() method to check if the current time falls within a specified time range. By defining start and end times and comparing them with the current time, isBetween() determines if the current time lies between the given times.

Example: The below example uses isBetween() Method to Check whether the current time is between 2 times in MomentJS.

Output:

Current Time: 05:17 PM
Is current time between 08:00 AM and 05:00 PM? false

Using isAfter() and isBefore() Methods

In this approach, we are using Moment.js's isAfter() and isBefore() methods to determine if the current time falls within a specified range. By checking if the current time is after the start time and before the end time, we can confirm whether it is between the two times.

Example: The below example uses the isAfter() and isBefore() Methods to Check whether the current time is between 2 times in MomentJS.

Output:

Current Time: 05:18 PM
Is current time between 08:00 AM and 05:00 PM? false

Using diff() Method for Time Difference Comparison

In this approach, we are using Moment.js's diff() method to compare the current time against the start and end times by calculating the difference in minutes. By checking if the current time is greater than or equal to the start time and less than or equal to the end time, we determine if it falls within the specified range.

Example: The below example uses the diff() Method for Time Difference Comparison to Check whether the current time is between 2 times in MomentJS.

Output:

Current Time: 05:19 PM
Is current time between 08:00 AM and 05:00 PM? false
Comment
Article Tags: