Moment.js Duration Format Plugin
Last Updated :
21 Jul, 2022
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:
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
Share your thoughts in the comments
Please Login to comment...