VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-extract-time-from-momentjs-object/

⇱ How To Extract time From MomentJS Object? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How To Extract time From MomentJS Object?

Last Updated : 23 Jul, 2025

MomentJS is a powerful library for handling dates and times in JavaScript. It's widely used for formatting, manipulating, and parsing dates. Often, while working with MomentJS, you might need to extract just the time part from a MomentJS object.

These are the approaches for extracting time from the MomentJS object:

Using format() Method

In this approach, we will use the format() method in MomentJS which is one of the most commonly used methods. It allows us to format the MomentJS object into a string and we can specify the format to get only the time.

Syntax:

momentObject.format(formatString);

Example: In this example, we are using the format() Method to extract time from MomentJS object.

Output:

24-hour format: 17:48:51
12-hour format: 05:48:51 PM

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

In this approach we will use MomentJS individual getter methods to retrieve specific components of time such as hours, minutes, and seconds. These methods can be used if we need to extract and manipulate these time components separately.

Example: In this example we are using the MomentJS individual getter methods to extract time from MomentJS object.

Output:

Hour: 17, Minute: 59, Second: 2

Using toISOString() Method

In this approach we will use the toISOString() method which converts the entire MomentJS object to a string in ISO 8601 format, which includes the time. We can then extract the time portion from this string using standard JavaScript string manipulation methods.

Example: In this example we are using the MomentJS methods to extract time from MomentJS object.

Output:

12:35:46
Comment
Article Tags: