Open In App

Moment.js Duration Format Plugin

Last Updated : 21 Jul, 2022
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The Duration plugin makes it possible to add a lot of formatting options to Moment Durations of the Moment.js JavaScript date library. This will make it possible to describe a duration as required by the user.

Write the below command in the terminal to install Duration Format Plugin:

npm install moment-duration-format
 

The following are some of the functions in this plugin:

  • format

The below examples will help to understand some of the methods of the Duration Format Plugin.

Example 1:

JavaScript
import moment from 'moment';
import format from 'moment-duration-format';

let duration = moment.duration(2, 'weeks');
console.log(duration.format("h:mm"));

Output:

 

Example 2:

JavaScript
import moment from 'moment';
import format from 'moment-duration-format';

let duration = moment.duration({
    seconds: 3,
    minutes: 3,
    hours: 3,
    days: 3,
    weeks: 3,
    months: '3',
    years: '3'
});
console.log(duration.format());

Output:

 

Reference: https://momentjs.com/docs/#/plugins/duration-format/


Next Article

Similar Reads