0

Hi all,

I’m building a web to book a common room.

It has a form with a begin date field and an ending date field.

You can add as many beginning dates an ending dates as you want.

When the data is submitted I have an array with all the beginning dates and another array with all the ending dates.

To clarify:

Begin dates             Ending dates
01-07-2013              03-07-2013
04-07-2013              10-07-2013

arrayBeginDates ["01-07-2013","04-07-2013"]

arrayEndDates ["03-07-2013","10-07-2013"]

The way the arrays a formed in my code:

var arrayBeginDates = [];
$(".beginDate").each(function(){
    arrayBeginDates.push($(this).val());
})

//and the same for ending dates, arrayEndDates

I need to validate that in the pairs begin-end dates the end date is greater than the begin date.

Meaning:

arrayEndDates [0] has to be greater than arrayBeginDates [0] otherwise error alert and exit arrayEndDates [1] has to be greater than arrayBeginDates [1] otherwise error alert and exit

and so on

How can I do that??

Thanks a ton!

Miguel Mas
  • 547
  • 2
  • 6
  • 23

2 Answers2

0

Try this:

var bDates = ["01-07-2013","04-07-2013"];
var eDates = ["03-07-2013","10-07-2013"];

for (var i = 0; i < bDates.length; i++) {
   if ((new Date(eDates[i].split('-').reverse().join(','))) > (new Date(bDates[i].split('-').reverse().join(',')))) {
      console.log('valid');
   } else {
      console.log('invaid');
   }
}
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
0

Something like the following:

arrayBeginDates.each( function(index, beginDate) {
    var begin = new Date(beginDate).setHours(0,0,0,0);
    var end = new Date(arrayEndDates[index]).setHours(0,0,0,0);

    if (begin > end)
        //handle Error
});

Note: This requires that arrayStartDates and arrayEndDates have the same number of elements (but then, your question doesn't make much sense if they don't).

Pat Lillis
  • 1,152
  • 8
  • 19