views:

33

answers:

2

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?

A: 

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 Date constructor.

The Date constructor can take the following arguments

new Date(year, month, day, hours, minutes, seconds, ms);

The being the day, hours, minutes, seconds and ms optional.

But you should remember to substract 1 from 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.:

var date = parseDate('09-26-2010', 'mm-dd-yyyy');
CMS
thanks it works!
cyberfly
A: 

Have you checked your code structure...It seems that your else is hanging without an if.

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;
    }                
  }                
}
Sidharth Panwar
the actual structure is correct. sorry it seems i make mistake while simplify the code to ask in this forum.
cyberfly
np, we all do it from time to time :)
Sidharth Panwar