views:

293

answers:

4

Hi I'm having a problem setting a date in as3

here is the code i'm using

    var endDate = new Date(2009,9,10);
trace (endDate);

the trace statement always shows the date as 1 month further on the the date I have added eg

10th Oct 2009 instead of 10th september 2009

Is there a way around this?

A: 

Yeah, dates are zero indexed in AS, so you'll need to subtract one

Chris Thompson
+3  A: 

The month is 0 index.

var endDate = new Date(2009,9-1,10);
Joel Hooks
The same imbecile who wrote that class thought it would be swell to have days 1-based at the same time. For optimal confusion, the original design had an American date order (mm,dd,yyyy) in the constructor too.
Wouter van Nifterick
A: 

0 indexed like the other said. Try and take a look at this post for more tips on the date object:

http://stackoverflow.com/questions/744057/how-can-you-save-time-by-using-the-built-in-date-class

Lillemanden
A: 

Yeah I thought that maybe the case. Minusing 1 from the date would be the easy option but I was thinking of more of a work around that i could use so that i don't have to do this.

I have tried creating variables for day, month and year then minusing 1 from the month before using them to make the date:

var day:Number="10";
var month:Number="9";
var year:Number="2009"

var adjMonth =month-1;

var endDate = new Date(year,adjMonth,day);

but this does not work. Anyone have a better (does not give errors) solution using this same concept?

you are still just "minusing 1 from the date" - it isn't a workaround, just more verbose. Your code above doesn't do anything my answer doesn't, so as to why it doesn't work it is probably a casting issue with the strings and number. Try making day, month, year adjMonth int types.
Joel Hooks
Ok that worked. Making them all int types solves the problem