J.Featherstone J.Featherstone - 1 month ago 9
Javascript Question

Comparing two Dates, Change date depending on other

So i have two dates a start date and end date, now obviously the start date cannot be after the end date.

I am using jquery to validate the input, so my idea was to see if the start date is greater than the end date if so change the end date to the date that is in the start date field.

The problem is i don't know how to compare then in a if statement that will automatically change the second date. Can anyone advice me on this matter?

Answer

Please try below code :-

function DateCheck()
{
  var StartDate= document.getElementById('txtStartDate').value;
  var EndDate= document.getElementById('txtEndDate').value;
  var eDate = new Date(EndDate);
  var sDate = new Date(StartDate);
  if(StartDate!= '' && EndDate!= '' && sDate> eDate)
    {
      document.getElementById('txtEndDate').val(StartDate);
      alert("Please ensure that the End Date is greater than or equal to the Start Date.");
      return false;
    }
}