0

So, I try to get a difference bewteen two times (not dates) but it doesn't work at all.

This is the javascript code I'm using now:

function getTimeBeforeClockIn() {
    $.ajax({
        url: '/ajax/get-trip-ideal-stop-time',
        type: 'GET',
        contentType: 'application/json',
        success: function (ideal_stop_time){
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = checkTime(m);
            s = checkTime(s);
            var cur_time = h + ":" + m + ":" + s;
            console.log(cur_time);
            console.log(ideal_stop_time);
            var stime = (new Date(new Date().toDateString() + ' ' + ideal_stop_time));
            var time_difference = cur_time - stime;
            console.log(time_difference);
            document.getElementById('ideal_time_for_stop').innerHTML = time_difference;
        }
    });

    var t = setTimeout(getTimeBeforeClockIn, 500);
}

The ajax call returns a string with the format hh:mm:ss so like 22:09:25

So I want the difference between a new Date(); object and a string (since the ajax call gets a string)

I want to get the difference between the current time and the time from the ajax call. This can be positive and negative. So if the time is negative, then it should get an output like - 00:08:25 format: hh:mm:ss else it needs an output with a +

hope someone can help me with this?

Robin
  • 1,567
  • 3
  • 25
  • 67
  • what is `checkTime` – Mulan Jun 09 '17 at 20:13
  • Possible duplicate of [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Heretic Monkey Jun 09 '17 at 20:15
  • Why don't you simply find the difference between `stime` and `today`? – Scott Sauyet Jun 09 '17 at 20:16
  • @MikeMcCaughan: I don't think that's a real duplicate. Perhaps techniques will overlap, but this clearly has to do with time of day. – Scott Sauyet Jun 09 '17 at 20:18
  • @MikeMcCaughan it is not a duplicate. I have a **string** and a **time**object, I need to know the difference between those two. – Robin Jun 09 '17 at 20:19
  • I would convert to seconds and operate with that. –  Jun 09 '17 at 20:22
  • Well, since there's no such thing as a time object, you have a Date object, so you'll need to get the difference between two Dates. There are several answers in the question which describe how to get more granular differences than days. There are other questions regarding how to parse and format dates. Combine them together and you'll get your answer. – Heretic Monkey Jun 09 '17 at 20:24
  • For parsing a time into a Date: https://stackoverflow.com/q/141348/215552; formatting: https://stackoverflow.com/q/8847109/215552 – Heretic Monkey Jun 09 '17 at 20:26
  • Something like this was asked here https://stackoverflow.com/q/1787939/8068435 – Ranjeet Jun 09 '17 at 20:41

1 Answers1

0

You want to do:

var time_difference=today-stime;

This will give you the number of seconds between those two Date objects. Then convert those seconds into the time format that you want.