Hi i have this javascript function
function test(){
var count = 0;
var date1 = $('#alternatestartdate').val();
var date2 = $('#alternateenddate').val();
var startDate = new Date(date1);
var endDate = new Date(date2);
var loop = true;
while (loop)
{
$('#pa').append('first test');
if (startDate<=endDate)
{
$('#pa').append('second test');
loop = true;
}
startDate.setDate(startDate.getDate()+1);
}
else
{
loop = false;
}
}
}
The problem is the function will not enter the if loop when tested in IE8. So only first test will be printed. second test will not be printed. Other browser all work fine. How to fix this?
The problem can be that IE is not taking the date format that you are getting from your inputs -since your code works correctly on other browsers-.
To avoid that, I would recommend you to parse the date string, to extract the numerical parts of it, and use the
Dateconstructor.The
Dateconstructor can take the following argumentsThe being the
day,hours,minutes,secondsandmsoptional.But you should remember to substract
1from the month number, because they are 0-based (0=Jan, 2=Feb, … 11=Dec).Of you could use a function like this one that I’ve made some time ago, which does all the job, and you can specify a format to parse your date, e.g.: