Open In App

Moment.js Precise Range Plugin

Last Updated : 17 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MomentJS is a JavaScript library that helps parsing, validating, manipulating and displaying date/time in JavaScript in a very easy way. This chapter will provide an overview of MomentJS and discusses its features in detail. Moment JS allows displaying of date as per localization and in human-readable format.

Precise Range Plugin: The Precise Range plugin is used to show proper, exact, human-readable representations of date and time ranges. It is mostly used to a difference between two dates with respect to days, months, years, and time.

The moment().preciseDiff() method is used to provide the difference between two dates we will provide as a parameter to both the methods respectively.

Syntax: 

moment(YYYY-MM-DD HH:MM:SS).preciseDiff(YYYY-MM-DD HH:MM:SS)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • String: It is the date from which the difference has to be calculated. It should be in the format of “yyyy-mm-dd”.

Return Value: It returns the difference between two dates in terms of the year, month, days, hours, minutes, and seconds in string format.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install moment
npm install moment-precise-range-plugin

Project Structure: The project structure will look like this:

 

Example 1: In this example, we will pass the first date in a specific format to moment() and call preciseDiff() on the moment() object by passing the second date as a parameter. We will finally get the difference between the two dates in string format.

app.js: Write down the below code in app.js file:

Javascript




const moment = require('moment');
const precisePlugin = require('moment-precise-range-plugin');
  
const result = moment("2017-01-01 12:00:00")
                   .preciseDiff("2022-03-04 16:05:06");
  
console.log(result)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

5 years 1 day 4 hours 5 minutes 6 seconds

Example 2: In this example, we will pass date in a specific format to moment() and will store the result of both dates in two distinct variables date1 and date2 respectively. After that, we will call the preciseDiff() method on the moment object by passing three values as parameters. The first is the date1 object, the second is date2 object, the third parameter is true. We will get the result in object format as we are sending the third parameter as true to preciseDiff() method which is being called on the moment object.

app.js: Write down the below code in app.js file:

Javascript




const moment = require('moment');
const precisePlugin = require('moment-precise-range-plugin');
  
let date1 = moment(
      '2022-01-01 11:59:59','YYYY-MM-DD HH:mm:ss'
);
let date2 = moment(
      '2022-08-15 12:00:00','YYYY-MM-DD HH:mm:ss'
);
  
let diff = moment.preciseDiff(date1, date2, true);
  
console.log(diff)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{
  years: 0,
  months: 7,
  days: 14,
  hours: 0,
  minutes: 0,
  seconds: 1,
  firstDateWasLater: false
}

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



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

Similar Reads