VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-remove-seconds-milliseconds-from-date-convert-to-iso-string-in-momemtjs/

⇱ How To Remove Seconds/ Milliseconds From Date Convert To ISO String In Momemt.js? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How To Remove Seconds/ Milliseconds From Date Convert To ISO String In Momemt.js?

Last Updated : 23 Jul, 2025

MomentJS is useful for handling and formatting dates and times in JavaScript. To remove seconds and milliseconds from a date and convert it to an ISO string, we can use MomentJS to manipulate and format the date. This allows you to customize the date output to meet specific requirements, such as removing less relevant time components.

These are the following approaches to Remove Seconds/ Milliseconds from Date convert to ISO String in Momemt.JS:

Run the below command before running the code in your local system:

npm i moment

Using startOf() and toISOStrin() methods

In this approach, we are using Moment.js's startOf() method to truncate the date to the start of the minute, which removes the seconds and milliseconds. The toISOString() method then converts this adjusted date to an ISO string.

Example: The below example uses startOf and toISOString to Remove Seconds/ Milliseconds from Date convert to ISO String in Momemt.JS.

Output:

Input Date: 2024-08-22T14:26:00+05:30
Output Date without seconds and milliseconds: 2024-08-22T08:56:00.000Z

Using set and toISOString() method

In this approach, we are using Moment.js's set() method to explicitly set the seconds and milliseconds to zero. This removes these components from the date. The toISOString method is then used to convert this adjusted date to an ISO string, resulting in a string without seconds and milliseconds.

Example: The below example uses set and format to Remove Seconds/ Milliseconds from Date convert to ISO String in Momemt.JS.

Output:

Input Date: 2024-08-22T14:28:00+05:30
Output Date without seconds and milliseconds: 2024-08-22T08:58:00.000Z
Comment
Article Tags: