Open In App

Moment.js Duration Format Plugin

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
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/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads