Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate difference between two dates using Node.js?

I need to calculate the datetime difference using the node.js. I am using the dateformat package to get current datetime format. My code is below:

var dateFormat = require('dateformat');
var prev_date=2017-08-09T19:00:58+0530;
var now = new Date();
var current_date=dateFormat(now, "isoDateTime");

Here I have already the previous date and also the current datetime also. I need to calculate the difference between two dates in hour, minute and second separately.

like image 793
satya Avatar asked Aug 09 '17 13:08

satya


People also ask

Can we subtract two dates in JavaScript?

Here, first, we are defining two dates by using the new date(), then we calculate the time difference between both specified dates by using the inbuilt getTime(). Then we calculate the number of days by dividing the difference of time of both dates by the no. of milliseconds in a day that are (1000*60*60*24).

How do I calculate the number of years between two dates in JavaScript?

var years = ((days)*(thisyear-birthyear)) /((number_of_long_years*366) + ((thisyear-birthyear-number_of_long_years)*365) );

How to calculate the time difference between two dates in JavaScript?

Calculate the time difference of two dates using date2.getTime () – date1.getTime (); Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.write (). Example 1: The following JavaScript program will illustrate the solution

How to calculate the number of days between two dates?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.write (). Example 1: The following JavaScript program will illustrate the solution

What is the difference between Node JS and NPM?

What is the difference between Node.js and NPM? Node is a runtime. The npm command line utility runs on the Node.js runtime. Running this registry is expensive, so Npm, Inc was formed to support it. What is the difference between jQuery, Ajax and Node.js? client-side Node.js is server-side JavaScript, used for developing server software.

How to calculate the number of days until Christmas Day in JavaScript?

Use Math.round (christmas () – present_date.getTime ()) Divided one day’s milliseconds to Calculate the result in milliseconds and then converting into days Example 2: This example we calculated the number of days until Christmas day. JavaScript is best known for web page development but it is also used in a variety of non-browser environments.


1 Answers

let firstDate = new Date("7/13/2016"),
    secondDate = new Date("09/15/2017"),
    timeDifference = Math.abs(secondDate.getTime() - firstDate.getTime());

console.log(timeDifference);
alert(timeDifference)

If you want the number of days between the two dates...

let differentDays = Math.ceil(timeDifference / (1000 * 3600 * 24));

console.log(differentDays);
alert(differentDays);

You could also use Moment.js https://momentjs.com/docs/

console.log(moment.utc(moment(firstDate,"DD/MM/YYYY HH:mm:ss").diff(moment(secondDate,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss"))
like image 144
Chris Hawkes Avatar answered Sep 30 '22 00:09

Chris Hawkes