views:

42

answers:

1

I am trying to get a month range to insert into a CAML query, ie: 2010-09-01 and 2010-09-30.

I have used the following code to generate these two values:

var month = "10/2010";
var monthArray = month.split("/");
var startDate = new Date(monthArray[1], monthArray[0]-1, 1);
var endDate = new Date(startDate);
endDate.setMonth(startDate.getMonth()+1, startDate.getDate()-1);

Running this code:

alert("month: " + month + 
      "\nstartDate: " + startDate.toDateString() + 
      "\nendDate: " + endDate.toDateString()); 

generates the correct dates (corporate policy requires IE7):

---------------------------
Windows Internet Explorer
---------------------------
month: 10/2010
startDate: Fri Oct 1 2010
endDate: Sun Oct 31 2010
---------------------------
OK   
---------------------------

However, when I attempt to parse into ISO 8601 format (for the CAML query), I get the wrong dates.

var endISO8601 = endDate.getUTCFullYear() + "-" + 
                 endDate.getUTCMonth() + "-" + 
                 endDate.getUTCDate() + "T19:59:00Z";
alert("endDate: " + endDate.toDateString() + 
      "\nendISO8601: " + endISO8601);

---------------------------
Windows Internet Explorer
---------------------------
endDate: Sun Oct 31 2010
endISO8601: 2010-9-31T19:59:00Z
---------------------------
OK   
---------------------------

I am not allowed to use Datejs, unfortunately.

+5  A: 

I think you are just forgetting to add one to the month (january is 0 in javascript)

ormuriauga
LOL! Thanks - I knew to subtract 1 when I was building the date but totally forgot I needed to add it back in when building my string.
Nathan DeWitt