1

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?

Anand Systematix
  • 632
  • 5
  • 15

5 Answers5

1

Have you tried looking at the Date Object and its methods? Specifically, you may want to checkout the getTime() method.

JS Dates

JS Date Formats

JS Date Methods

Something like the following:

if( (new Date(dateOne).getTime() > new Date(dateTwo).getTime()))
{
    // Compare and do stuff
}
User 5842
  • 2,849
  • 7
  • 33
  • 51
1

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;
    }
}
Anand Systematix
  • 632
  • 5
  • 15
1

After messing around with things, i found the answer :)

             if ( $('#id_start_0').val() > $('#id_end_0').val()){
                  $('#id_end_0').val( $('#id_start_0').val());
              }
  • $('#id_start_0').val() will be treated as a text not date, I think it will not work. Please test it properly. – Anand Systematix Oct 19 '16 at 12:23
  • @Anand_Systematix this is what i thought, everything seems to be working fine though, when the start date is above the end date, the end date is changed to what ever the start date is. There seems to be no problem comparing the values and seeing who is the highest date. which is strange. Iv testing with year, month and dates both ways. It seems to be working. Thank you for mentioning it though i will keep my eye on it. – J.Featherstone Oct 19 '16 at 12:44
1
HTH,


var startDate= '2013-09-04';
var endDate = '2013-09-03';


        //if startDate is greater then endDate
if( (new Date(startDate).getTime() > new Date(endDate).getTime()))
{ 
     //then replacing the endDate with startDate
    endDate=endDate.replace(endDate,startDate);
    alert(endDate)
}
Khusboo
  • 117
  • 1
  • 1
  • 9
  • Please edit your answer to include more information. Code-only and "try this" answers are discouraged because they contain no searchable content, and don't explain why someone should "try this". – BrokenBinary Oct 19 '16 at 16:15
  • Thank you for your suggestion :), I will do it. – Khusboo Oct 20 '16 at 08:51
1

Hope this helps:

var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
    text = "Today is before January 14, 2100.";
} else {
    text = "Today is after January 14, 2100.";
}
Suyash Gandhi
  • 926
  • 6
  • 24