-1

I want to create a time differance calculator between two given time and a date number. Also I want to tell you that I have made 4 input fields: time1, time2, days and timediff.

The time format that I want is very simple, ex: 700 for 7:00, 932 for 9:32, 1650 for 16:50 and so on...

Now I also have the day input field, where I can put in some numbers, ex: 1(for the first day, 2 for the second day and so on).

I want that if I put 1(first day) to not calculate the days, only to calculate the timediff, because it's the same day. But if I put 2 on the date field then 24hours(one day) to be added to the timediff, if I put 3 48hours(two days) to be added and so on...

I have something like this, but is not working well, when I change the day the result is mess up...

var time1 = document.getElementById('indulas').value;
var time2 = document.getElementById('erkezes').value;
var day = document.getElementById('nap').value;
if (day > "1"){ day * 2400 };
var time = (time2 * day) - time1;
document.getElementById('ido').value = time;
styven
  • 51
  • 1
  • 9
  • 2
    For easy time calculation, I would recommend the momentjs libary: http://momentjs.com/ – Jacob van Lingen Jun 17 '14 at 11:41
  • I want to resolve the problem very simple!! – styven Jun 17 '14 at 12:09
  • I would recommend you have a long, hard look at following link: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). Getting Date 'right' is quite difficult and judging from the code you posted, you might be more helped by following Jacobs suggestion. Also, you might want to post a link where one can play around with your code - jsfiddle/ jsbin/ plunker – flaky Jun 17 '14 at 13:45

1 Answers1

1

This cannot be solved as subtraction of two times in format HHMM because hour has only 60 minutes (eg. 900 - 732 = 168 does not equal to the correct 1 hour and 28 minutes)

This script should work

function pad(num, size) { // num = number which gets the leading zeros; size = number of digits the string will have (in your case 2)
    var s = num.toString(); // convert integer to string
    while (s.length < size) s = "0" + s; // if number of digits is smaller than requested, add leading zeros
    return s; // return the number (as string) with leading zeros
}

var time1 = document.getElementById('indulas').value.match(/.{2}/g),
    // split string each 2 characters ("0932" -> ["09", "32"])
    time2 = document.getElementById('erkezes').value.match(/.{2}/g),
    // day 1 is today, day 2 - 1 adds 24 hours
    days = parseInt(document.getElementById('nap').value)-1,

    // start date on day 0 at HH, MM
    startDate = new Date(0, 0, 0, parseInt(time1[0]), parseInt(time1[1])),
    // end date on day 0 + days at HH, MM
    endDate = new Date(0, 0, days, parseInt(time2[0]), parseInt(time2[1])),
    // subtract dates in milliseconds
    diff = endDate.getTime() - startDate.getTime(),

    // hour and minute in miliseconds
    hour_in_mili= 1000 * 60 * 60,
    minute_in_mili = 1000 * 60,

    // calculate number of hours in diff time
    hours = Math.floor(diff / hour_in_mili),
    // calculate number of minutes in the diff time what left after subtracting the hours
    minutes = Math.floor((diff - (hours * hour_in_mili)) / minute_in_mili);

    /*
       if indulas.value = "0932", erkezes.value = "1650", nap.value = "1"
       script shows "7:18"
    */

document.getElementById('ido').value = hours + ":" + pad(minutes, 2);
user2781994
  • 469
  • 1
  • 5
  • 12
  • thank you very much!! I realy appreciate your effort! finally someone that really analyze my needs before answering! only one little problem, why when I enter time1=0630 and time2=1633 it's giving me the timediff=10:3 ?? – styven Jun 17 '14 at 15:29
  • 1
    @user3488958 because minutes (3 in this case) is an integer. If you want 03, you have to convert it into string and add leading zero. Check [this](http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript#2998822)! – user2781994 Jun 17 '14 at 15:38
  • how can I do it in my case? can you please help me out? – styven Jun 17 '14 at 15:42
  • thank you very much!! I realy appreciate your help! best regards! :) – styven Jun 17 '14 at 16:01