A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: how does this work?

  1. #1
    Senior Member
    Join Date
    Mar 2003
    Location
    wishconsin
    Posts
    151

    how does this work?

    ericlin posted this wonderful code for retrieving the number of days on any givin month:

    //how may days in the month of 2004 Feb
    d = new Date(2004, 2, 0);
    trace(d.getDate());
    trace(d);

    how does it work that placing a zero in the third param of new Date retreives the total number of days? It's not in flash's help, but it seems like a real important feature? I'm just curious how it works, and where to find all the other "secret actionscript"

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    This behavior is documented in Actionscript the Definitive Guide, by Colin Moock which has this to say about constructing a Date object:

    month:

    An integer specifying the month, from 0 (January) to 11 (December), not from 1 to 12. Required when using the year,...ms constructor format. Out-of-range months are carried over to the next or previous year. For example, a month argument of 13 is treated as February of the following year.

    day:

    An optional integer specifying the day, from 1 to 31. Defaults to 1 if not specified. Out-of-range days are carried over to the next or previous month. For example, September 31 is treated as October 1, September 0 is treated as August 31, and September -1 is treated as August 30.
    So, in your example above, although it looks to the casual user like you are asking for "February 0" you are actually asking for "March 0". According to the above documentation, the 0 rolls back to the last day in the previous month (which will be either Feburary 29 or Feburary 28). Then, you are asking for the day number, via getDate(), so you will get 28 or 29.

    The above book is a great source for similar arcana which is not documented in the Macromedia help.

    This is a nice catch by EricLin by the way - I have previously solved this problem on this very board by writing a leap-year detector... This is much more elegant!
    Last edited by jbum; 08-05-2004 at 06:31 PM.

  3. #3
    Senior Member
    Join Date
    Mar 2003
    Location
    wishconsin
    Posts
    151
    ahhhh. very nice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center