Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get difference between 2 dates in the format : HH:MM:SS


suppose [ from date ]= 03-04-2017 08:00:00 (this is in dd-MM-yyyy hh:mm:ss format)
[ to date ]=04-04-2017 09:00:00 (this is in dd-MM-yyyy hh:mm:ss format)


now the output will be : (todate -from date) = 25:00:00

how to get this value ?

please help . i tried a a lot. but didn't find any solutions.

What I have tried:

var from_date =new Date( "03-04-2017 08:00:00");
var to_=new Date( "04-04-2017 09:00:00");
alert(now);
alert(then);
Posted
Updated 2-Apr-17 23:17pm

1 solution

function getDateObject(datestr) {
          var parts = datestr.split(' ');
          var dateparts = parts[0].split('-');
          var day = dateparts[0];
          var month = parseInt(dateparts[1]) - 1;
          var year = dateparts[2];
          var timeparts = parts[1].split(':')
          var hh = timeparts[0];
          var mm = timeparts[1];
          var ss = timeparts[2];
          var date = new Date(year, month, day, hh, mm, ss, 00);
          return date;
      }

      function gettimediff(t1, t2) {
          var t1val = Number( t1.getHours() * 60 + t1.getMinutes());
          var t2val = Number(t2.getHours() * 60 + t2.getMinutes());
          var min = Math.floor((t2val - t1val) % 60);
          var hours = parseInt((t2 - t1) / (1000 * 60 * 60));
          return (hours + ':' + min + ':00');
      }

      var from = '03-04-2017 08:00:00';
      var to = '04-04-2017 09:00:00';
      from = getDateObject(from);
      to = getDateObject(to);

      alert(gettimediff(from, to)); //25:0:00
 
Share this answer
 
Comments
Mahesh Pattnayak 5-Apr-17 4:58am    
thank u sir..
Karthik_Mahalingam 5-Apr-17 5:03am    
welcome,
if it works please close this post
Mahesh Pattnayak 5-Apr-17 5:42am    
sir please help on this issue :

https://www.codeproject.com/Questions/1180539/How-to-assign-session-value-in-javascript-webcam-d

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900