Why does my date/time checker fail with certain input times?

I have a date/time checker that checks if two dates/times overlap. If it does, it shows a warning. For much of the time it works. However if the starttime is a single digit and the endtime is a double digit it fails (For example starttime=6:30AM and endtime=10:30AM).

It also fails if the starttime is an “AM” time and the endtime is a “PM” time such as starttime=6:30AM and the endtime=6:30PM. I initially thought it may be due to the times being non-military time. So I added a function that I found from the web to convert the time to military time. So that the user sees the AM/PM time but the conditional function calculates using the military time. Unfortunately the problem still exists. I think that either my logic is not correct for the conditional statements or possibly the way I set up the script. For debugging, I added several alerts. They all seem to be giving the proper information such as the correct time in military format except for the last alert which alerts “Problem” when it should not. Which shows that this script still fails. I am now confused. :disappointed: Can somebody tell me how to fix this thing? I would like for:

  1. fire alert when times overlap on any given day, and

  2. fire alert if times go beyond the possible times on any given day (the endtime can not go past 11:59PM if startdate and enddate are the same day).

Here is the code and thanks for your help;

<script>      
// Anonymous Function
$(function () {
    var dateA;
    var dateB;
    var timeA;
    var timeB;      
    var apple;
    var banana;

    // Check dates to see if they are present and match
    $("#startdate, #enddate").on("change.dates",function() { 
        dateA = $('#startdate').val();
        dateB = $('#enddate').val();            

        if (((dateA).length && (dateB).length) && (dateA == dateB)) {
            alert("Apple");         
            apple ="true";
        } else {
            apple ="false";
        }           
    }); 
    // Check times to see if they are present and convert to military time
    $("#starttime, #endtime").on("change.times",function() {            
        timeA = (convertTo24Hour1($("#starttime").val().toLowerCase()));       
        timeB = (convertTo24Hour2($("#endtime").val().toLowerCase()));              
        alert("A - " + timeA);
        alert("B - " + timeB);

        function convertTo24Hour1(timeA) {
            var hoursA = parseInt(timeA.substr(0, 2));              
            if(timeA.indexOf('am') != -1 && hoursA === 12) {
                timeA = timeA.replace(12, 00);
            }
            if(timeA.indexOf('pm') != -1 && hoursA < 12) {
                timeA = timeA.replace(hoursA, (hoursA + 12));
            }
            alert("Papaya");                                
            return (timeA.replace(/(am|pm)/, ''));              
        }

        function convertTo24Hour2(timeB) {
            var hoursB = parseInt(timeB.substr(0, 2));              
            if(timeB.indexOf('am') != -1 && hoursB === 12) {
                timeB = timeB.replace(12, 00);
            }
            if(timeB.indexOf('pm') != -1 && hoursB < 12) {
                timeB = timeB.replace(hoursB, (hoursB + 12));
            }
            alert("Mango");             
            return (timeB.replace(/(am|pm)/, ''));              
        }

        if (((timeA, timeB).length && (timeA >= timeB))) {                      
            alert("Banana");            
            banana = "true";                
        } else {
            banana = "false";               
        }           
    });     

    // Check dates to see first two scripts were "true" (same dates and overlapping times)
    $("#startdate, #enddate, #starttime, #endtime").on("change",function() {            

        if ((apple == "true") && (banana == "true")) {              
            alert("Problem");               
        } else if ((apple == "false") || (banana == "false")) {                 
            alert("No Problem");
        }
    });     
}); 
</script>

<form>
  <fieldset>
    <ul>
      <li>
        <p><input type="text" id="startdate"></p>
      </li>

      <li>
        <p><input type="text" id="starttime"> </p>
      </li>

      <li>
        <p><input type="text" id="enddate"> </p>
      </li>

       <li>
        <p><input type="text" id="endtime"></p>
       </li>
     </ul>
  </fieldset>
</form>

Is “6:12” less than, greater than, or equal to “10:20”?
Don’t think about them as numbers - think about them as strings…
Why is “abc” less than “baaaaaa” ?

Or if you need a bigger hint:

Why do you try to set your midnight hours to “00” instead of “0”? What does that mean you should be doing for “6:30” ?

Hello m_hutley, thanks for the reply. I have attempted to convert values to integers with no success. It actually breaks the code. In addition the code works for well over 50% of the values. Which sounds strange to me if “abc” is being compared against something like “baaa” . How would this code be corrected to convert values from strings to integers? Thanks

It looks like you might be on the wrong track.

Instead, you need to focus on using leading zeros so that “6:12” ends up being “06:12” instead. And, so that “10:2” ends up being “10:02” as well.

1 Like

Is there a good reason you are not using momentJS or dateFNS libraries here?

1 Like

or just a good old fashioned DateTime…

But I was working within the user’s statements.

Thank you Mr. Wilkins, that definitely was the solution. You are incredible. Thanks for actually helping me without the riddles and rhymes. You are a great teacher.

1 Like

Thanks for the reply. How am I suppose to learn programming if I use momentJS or dateFNS?

Here is a counter question - why are you learning programming?

I decided to learn programming to enhance my mind and add to my science knowledge and skills.

Don’t forget to practice it regularly :slight_smile: :+1:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.