When a user selects a date from my DateTimePicker it has the date like 01/07/2021, when comparing to the current day to ensure it's not a date in the past, my check formats the date like 1/7/2021 and then my code throws the error.
In this scenario it shouldn't throw an error as the dates are exactly the same, just for the additional 0's. What am I doing wrong?
HTML -
<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1" />
<div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
<div id="dateTime" class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
JQuery -
//create datetimepicker
$(function () {
$('#datetimepicker1').datetimepicker({
sideBySide: true,
format: 'L',
// restrict past dates being selected
minDate: new Date()
});
});
var dateTime = $("#datetimepicker1").data().date;
// get current Date to check against user input
var currDate = new Date();
var dd = currDate.getDate();
var mm = currDate.getMonth()+1;
var yyyy = currDate.getFullYear();
currDate = mm + '/' + dd + '/' + yyyy;
// check if user date is in the past. If in past then throw error & fail.
if (dateTime < currDate)
{
// throw error
alert('Error - Invalid Date. All dates must be either in the present or future.')
$("#datetimepicker1").datetimepicker("clear");
return false;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…