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. Can somebody tell me how to fix this thing? I would like for:
-
fire alert when times overlap on any given day, and
-
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>