Time comparison in for Loop

Viewed 89

I am facing a weird issue. Below is the code to create different time slots in Javascript:

function secondsToTime(secs) {
  var hours = Math.floor(secs / (60 * 60));
  var ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12 || 12;

  var divisor_for_minutes = secs % (60 * 60);
  var minutes = Math.floor(divisor_for_minutes / 60);

  var userfriendlytime = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + " " + ampm;

  return userfriendlytime;
}



$(document).ready(function() {

  var ptc_id = "236051";
  var ptc_date = "04/30/2021";
  var ptc_from_time = "5400";
  var ptc_to_time = "46800";
  var ptc_duration = "07";
  var ptc_duration_in_seconds = parseFloat(ptc_duration) * 60;
  var ptc_passing_time = "03";
  var ptc_passing_time_in_seconds = parseFloat(ptc_passing_time) * 60;

  var timefrom = ptc_from_time;
  var timeto = ptc_to_time;

  var slottimeto = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

  for (time = timefrom; time <= timeto;) {

    var slotfromtime = parseFloat(timefrom);
    var slottotime = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

    var a_id = slotfromtime + "" + slottotime;

    var slottime = secondsToTime(slotfromtime) + " - " + secondsToTime(slottotime)

    console.log(slottime);
    timefrom = parseFloat(slottotime) + parseFloat(ptc_passing_time_in_seconds);
    time = timefrom;

    if (parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds) > timeto) {
      break;
    }

  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I am storing the time in seconds. The above code doesn't work, but when I change the timings like reducing from time and to time, the codes start working. Here is the working code:

function secondsToTime(secs) {
  var hours = Math.floor(secs / (60 * 60));
  var ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12 || 12;

  var divisor_for_minutes = secs % (60 * 60);
  var minutes = Math.floor(divisor_for_minutes / 60);

  var userfriendlytime = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + " " + ampm;

  return userfriendlytime;
}



$(document).ready(function() {

  var ptc_id = "236051";
  var ptc_date = "04/30/2021";
  var ptc_from_time = "10800";
  var ptc_to_time = "46800";
  var ptc_duration = "07";
  var ptc_duration_in_seconds = parseFloat(ptc_duration) * 60;
  var ptc_passing_time = "03";
  var ptc_passing_time_in_seconds = parseFloat(ptc_passing_time) * 60;

  var timefrom = ptc_from_time;
  var timeto = ptc_to_time;

  var slottimeto = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

  for (time = timefrom; time <= timeto;) {

    var slotfromtime = parseFloat(timefrom);
    var slottotime = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

    var a_id = slotfromtime + "" + slottotime;

    var slottime = secondsToTime(slotfromtime) + " - " + secondsToTime(slottotime)

    console.log(slottime);
    timefrom = parseFloat(slottotime) + parseFloat(ptc_passing_time_in_seconds);
    time = timefrom;

    if (parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds) > timeto) {
      break;
    }

  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

If you notice, I have just changed ptc_from_time.

2 Answers

Your program won't work correctly because you are using alphanumeric values to perform a comparison which should be numeric:

time <= timeto

Make sure that those variables contain always numeric values. Moreover, there is no need to use alphanumeric values that are going to be converted immediately to numeric. Keep your design as simple as possible: Use numeric constants and values.

Actually you have used double quotes for ptc_from_time and ptc_to_time and act as string literal, so the condition(time <= timeto) of both is false in first iteration because the first letter 5(time) is greater than first letter 4(timeto) which is not a number(string here) so remove the double quotes from your integer values. See the snippet below.

function secondsToTime(secs)
{
    var hours = Math.floor(secs / (60 * 60));
    var ampm = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12 || 12;
   
    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);

    var userfriendlytime = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + " " + ampm;
    
    return userfriendlytime;
}



$(document).ready(function() {

    var ptc_id = "236051";
    var ptc_date = "04/30/2021";
    var ptc_from_time = 5400;
    var ptc_to_time = 46800;
    var ptc_duration = 7;
    var ptc_duration_in_seconds = parseFloat(ptc_duration) * 60;
    var ptc_passing_time = 3;
    var ptc_passing_time_in_seconds = parseFloat(ptc_passing_time) * 60;

    var timefrom = ptc_from_time;
    var timeto = ptc_to_time;

    var slottimeto = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

    for (time = timefrom; time <= timeto;) { 

        var slotfromtime = parseFloat(timefrom);
        var slottotime = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

        var a_id = slotfromtime + "" + slottotime; 

        var slottime = secondsToTime(slotfromtime) + " - " + secondsToTime(slottotime)

        console.log(slottime);
        timefrom = parseFloat(slottotime) + parseFloat(ptc_passing_time_in_seconds);
        time = timefrom;

        if (parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds) > timeto) { break; }

    }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related