Open In App

Moment.js Round Plugin

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Round plugin is a moment.js plugin that will round the date and time to the specified range.

Write the below command in the terminal to install Round Plugin:

npm install moment-round

 

The following are some of the functions in this plugin:

  • round
  • floor
  • ceil

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

Example 1:

Javascript




import moment from 'moment';
import round from 'moment-round';
  
let day="2010-02-28 13:40:56"
let date=moment(day);
console.log('Before Rounding: ')
console.log(
    date.format("dddd, Do MMM YYYY, h:mm:ss A")
);
console.log('After Rounding: ')
console.log(
    date.round(5, 'seconds')
        .format("dddd, Do MMM YYYY, h:mm:ss A")
);


Output:

 

Example 2:

Javascript




import moment from 'moment';
import round from 'moment-round';
  
let day=Date.now();
let date=moment(day);
console.log("Before Rounding:")
console.log(
    date.format("DD/MM/YYYY-H:mm:ss")
);
console.log("After Rounding:")
console.log(
    date.ceil(21, 'hours')
        .format("DD/MM/YYYY-H:mm:ss")
);


Output:

 

Reference: https://momentjs.com/docs/#/plugins/round/



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

Similar Reads