Node.js calculate datetime difference in hours minutes and seconds moment.js

Node.js calculate datetime difference in hours minutes and seconds moment.js

In this post we will give you information about Node.js calculate datetime difference in hours minutes and seconds moment.js. Hear we will give you detail about Node.js calculate datetime difference in hours minutes and seconds moment.jsAnd how to use it also give you demo for it if it is necessary.

In this Node.js tutorial, I will tell you how to calculate datetime difference in days, hours, minutes and seconds using moment.js with example.

You can use moment in browser and in Node.js to calculate date difference.


Install Moment in Node.js

Run the npm command to install dependencies of Moment.js

npm install moment --save

Now require this package :

var moment = require("moment");



Calculate datetime difference in days

  1. functioncalculateDays(startDate,endDate)
  2. {
  3. var start_date =moment(startDate,'YYYY-MM-DD HH:mm:ss');
  4. var end_date =moment(endDate,'YYYY-MM-DD HH:mm:ss');
  5. var duration = moment.duration(end_date.diff(start_date));
  6. var days = duration.asDays();
  7. return days;
  8. }
function calculateDays(startDate,endDate)
{
   var start_date = moment(startDate, 'YYYY-MM-DD HH:mm:ss');
   var end_date = moment(endDate, 'YYYY-MM-DD HH:mm:ss');
   var duration = moment.duration(end_date.diff(start_date));
   var days = duration.asDays();       
   return days;
}

Above function will return the number of days between two given dates.

Moment have various function that return the number of weeks (0 – 4), number of hours (0 – 23), number of minutes (0 – 59), number of months (0 – 11) etc.

moment.duration().asDays(); // get the length of the duration in days
moment.duration().asHours(); // get the length of the duration in hours
moment.duration().asWeeks(); // get the length of the duration in weeks
moment.duration().asMonths(); // get the length of the duration in months
moment.duration().asYears(); // get the length of the duration in years

If you have any subscription and you want to know the remaining days from current day :

moment(subs_ends_at).diff(new Date(),'days');

subs_ends_at variable hold the expiry date.

A complete list of moment functions are given below :




























S.NoMoment FunctionResult
1moment()2017-08-31T06:53:16.470Z
2moment().unix()1504162446
3moment().format(‘MMMM Do YYYY, h:mm:ss a’)August 31st 2017, 11:57:57 am
4moment().format(‘D’)31
5moment().format(‘dddd’)Thursday
6moment().format(“MMM Do YY”)Aug 31st 17
7moment().format()2017-08-31T12:01:51+05:30
8moment(“20131231”, “YYYYMMDD”).fromNow()4 years ago
9moment(“20200722”, “YYYYMMDD”).fromNow()in 3 years
10moment().startOf(‘day’).fromNow()12 hours ago
11moment().endOf(‘day’).fromNow()in 12 hours
12moment().startOf(‘hour’).fromNow()16 minutes ago
13moment().subtract(10, ‘days’).calendar()08/21/2017
14moment().calendar()Today at 12:16 PM
15moment().add(1, ‘days’).calendar()Tomorrow at 12:16 PM
16moment.locale()en
17moment().format(‘LT’)12:17 PM
18moment().format(‘LTS’)12:17:52 PM
19moment().format(‘L’)08/31/2017
20moment().format(‘l’)8/31/2017
21moment().format(‘LL’)August 31, 2017
22moment().format(‘ll’)Aug 31, 2017
23moment().format(‘LLL’)August 31, 2017 12:19 PM
24moment().format(‘lll’)Aug 31, 2017 12:20 PM
25moment().format(‘LLLL’)Thursday, August 31, 2017 12:20 PM
26moment().format(‘llll’)Thu, Aug 31, 2017 12:20 PM

Moment.js plugin is very useful to parse, validate, manipulate and display dates.

Label :

jQuery

How To

Web Development

Node.js

JavaScript

Moment.js

Hope this code and post will helped you for implement Node.js calculate datetime difference in hours minutes and seconds moment.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

46  +    =  56

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US