Time difference and convert into hours and minutes in javascript
Asked Answered
U

5

10

I am having the time values as follows starttime like : 09:00:00 , endTime like : 10:00:00 ; here no date value is needed. so this values need to calculate difference and convert into hours and minutes,seconds.

I had tried with :

var test = new Date().getTime(startTime); 
var test1 = new Date().getTime(endTime);
var total = test1 - test;

Some time am getting NaN and 1111111 some digit format.

How can I convert into HH:MM:SS, or any other way to find time difference.

Ul answered 25/10, 2013 at 7:21 Comment(2)
Use moment.js for this instead of relying on javascripts built in. It will help you loads!Biddable
hi thanks for the comment.how to use and convert the values in hh:mm:ssUl
N
20

You can take a difference of the time values:

var diff = test1.getTime() - test.getTime(); // this is a time in milliseconds
var diff_as_date = new Date(diff);
diff_as_date.getHours(); // hours
diff_as_date.getMinutes(); // minutes
diff_as_date.getSeconds(); // seconds
Nadiya answered 25/10, 2013 at 7:36 Comment(7)
Hi i tried with that but am getting the error as Object 1382687270743 has no method 'getTime'.The error points the places in test1.getTime().please help me how to solve the problem.Ul
you should be using test = new Date(startTime) and test1 = new Date(endTime)Nadiya
if am using the before you had suggested again am getting error while printing the diff.getHours() as Object NaN has no method 'getHours' .how to solve the problem.Ul
If new Date(startTime) is giving you an error, try test = new Date('1970-01-01 ' + startTime)Nadiya
hi again am getting error as Object NaN has no method 'getHours' while am using new Date('1970-01-01' + startTime) like that.please help me.Ul
Thanks for the answer.it must be something like below right? diff_as_date.getHours(); // hours diff_as_date.getMinutes(); // minutes diff_as_date.getSeconds(); // secondsDorcasdorcea
Another issue I've faced is that the new Date(diff) returned date with the timezone offset (in my case, 3 hours). I used getUTCHours(), getUTCMinutes() and getUTCSeconds() to get the right values.Raddled
C
10

    var startTime = "09:00:00";
    var endTime = "10:30:00";
    
    var todayDate = moment(new Date()).format("MM-DD-YYYY"); //Instead of today date, We can pass whatever date        

    var startDate = new Date(`${todayDate} ${startTime}`);
    var endDate = new Date(`${todayDate } ${endTime}`);
    var timeDiff = Math.abs(startDate.getTime() - endDate.getTime());

    var hh = Math.floor(timeDiff / 1000 / 60 / 60);   
    hh = ('0' + hh).slice(-2)
   
    timeDiff -= hh * 1000 * 60 * 60;
    var mm = Math.floor(timeDiff / 1000 / 60);
    mm = ('0' + mm).slice(-2)

    timeDiff -= mm * 1000 * 60;
    var ss = Math.floor(timeDiff / 1000);
    ss = ('0' + ss).slice(-2)
    
    alert("Time Diff- " + hh + ":" + mm + ":" + ss);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.28.0/moment.min.js"></script>
Contracture answered 25/10, 2013 at 7:37 Comment(2)
hi thanks for the comments.but in declaration var startDate = new Date("January 1, 1970 " + startTime); in this declaration with out giving date how to calculate.please help meUl
hi hongchae. how difference the two different times.for sample time1 = 02:45 and time2 = 00:55. this is i need to minius the times and convert into hours and mintues.can any one help me. i had tried with var sp = time1.split(":"); var sp1 = time2.split(":"); var th = sp[0];var tm = sp[1]; var tth = sp1[0];var ttm = spl[1]; var finalh = 1 * sp[0] - 1 * sp1[0]; var finalmin = 1 * sp[1] - 1 * sp1[1]; this working fine if the minutes sp greater than sp1 but other cases am getting minus values as minutes . how to convert the values into hours and minutes.please help me.Ul
C
1
function diff(start, end) {
start = start.split(":");
end = end.split(":");
var startDate = new Date(0, 0, 0, start[0], start[1], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);

// If using time pickers with 24 hours format, add the below line get exact hours
if (hours < 0)
   hours = hours + 24;

return (hours <= 9 ? "0" : "") + hours + ":" + (minutes <= 9 ? "0" : "") + minutes;
}
Cyme answered 22/3, 2019 at 6:57 Comment(0)
O
0

in var timeDiff = Math.abs(startDate.getTime() - endDate.getTime()); we do not get the sign -, if endDate.getTime() is greater. how we can get the sign to?

Ovation answered 13/9, 2022 at 6:34 Comment(0)
T
0

To get days , hours and minutes you can use the following code.

function getDurationBetweenTwoDates(date1, date2) {
  if (date1 && date2) {
    let diff = Math.abs(date1 - date2);
    let day = Math.floor((((diff / 1000) / 60) /60) / 24);
    diff = diff - (day * 1000 * 60 * 60 * 24);
    let hour = Math.floor(((diff / 1000) / 60) / 60);
    diff = diff - (hour * 1000 * 60 * 60);
    let minute = Math.floor(((diff / 1000) / 60));
    return {
      day, hour, minute
    }
  }
}
Tragic answered 25/7 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.