Skip to content Skip to sidebar Skip to footer

Difference In Hours Between Two Dates And Times By Javascript

I have three input fields of type text in my html file. First input field is called OLD DATE & TIME. Second input field is called NEW DATE & TIME. Third Input field is cal

Solution 1:

If you want to use plain javascript then you can do calculations in this way..

var fromDate = parseInt(newDate(oldDate).getTime()/1000); 
    var toDate = parseInt(newDate(newDate).getTime()/1000);
    var timeDiff = (toDate - fromDate)/3600;  // will give difference in hrs

Solution 2:

Please use moment.js. this also can changing formate of date and time.

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

moment.utc(moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

Solution 3:

The Date object has a getTime() method which returns the number of milliseconds since midnight Jan 1, 1970. You could create a Date object for the two dates you're dealing with, call getTime() on each, subtract the difference and convert the result to hours.

Post a Comment for "Difference In Hours Between Two Dates And Times By Javascript"