Date object's getDay returns wrong date for Feb in any leap year

Discussion in 'Javascript' started by David Woodward, Jan 31, 2004.

  1. When I do the following (the date represents 2/1/2004) getDay returns
    1, and it should be returning 0. It's the only month for which we see
    this behavior.

    var oDate = new Date()
    oDate.setYear(2004)
    oDate.setMonth(1)
    oDate.setDate(1)

    iDay = oDate.getDay()

    Is this a know problem or an expected result?
    David Woodward, Jan 31, 2004
    #1
    1. Advertising

  2. Re: Date object's getDay returns wrong date for Feb in any leapyear

    (David Woodward) writes:

    > When I do the following (the date represents 2/1/2004)


    Which date is that? Second of January or first of February? (Yes, I
    know, but only because your remaning text is less ambiguous than this
    explanatory note :)

    Did you try it three days ago? It would have worked then.

    > getDay returns
    > 1, and it should be returning 0. It's the only month for which we see
    > this behavior.


    Tomorrow (on the 31th) you will see it for all 30 day months too.

    Have you tried debugging at all? Inserting alert statments between
    each line to see the current value of oDate? It would be informative.

    > var oDate = new Date()


    Here oDate is 31th of January 2004 for me now.

    > oDate.setYear(2004)


    No change

    > oDate.setMonth(1)


    Now you set the month to February. That means that the date is now
    supposed to be the 31th of February 2004 . Since that doesn't exist,
    the Date-object autocorrects it to the 2rd of March 2004 (two days
    after the 29th of February)

    > oDate.setDate(1)


    Now it is 1st of March 2004 .

    > iDay = oDate.getDay()


    1st of March 2004 is a Monday.

    > Is this a know problem or an expected result?


    Expected result. Never assign parts of a date separatly, especially
    date in month and month. Use
    oDate.setMonth(1,1);
    or better yet
    oDate.setYear(2004,1,1);
    (better because it won't fail on the 29th of February, like, e.g.,
    var myDate = new Date(2003,1,1); // some date in 2003
    myDate.setMonth(1,29);
    myDate.setYear(2004);
    result: 1st of March 2004)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 31, 2004
    #2
    1. Advertising

  3. David Woodward wrote:

    > When I do the following (the date represents 2/1/2004) getDay returns
    > 1, and it should be returning 0. It's the only month for which we see
    > this behavior.
    >
    > var oDate = new Date()
    > oDate.setYear(2004)
    > oDate.setMonth(1)
    > oDate.setDate(1)
    >
    > iDay = oDate.getDay()
    >
    > Is this a know problem or an expected result?


    Month values are zero based. January = 0, February = 1.

    Read the documentation:
    http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html#1193137
    ExGuardianReader, Jan 31, 2004
    #3
  4. David Woodward

    LJL Guest

    (David Woodward) wrote in
    news::

    > When I do the following (the date represents 2/1/2004) getDay returns
    > 1, and it should be returning 0. It's the only month for which we see
    > this behavior.
    >
    > var oDate = new Date()
    > oDate.setYear(2004)
    > oDate.setMonth(1)
    > oDate.setDate(1)
    >
    > iDay = oDate.getDay()
    >
    > Is this a know problem or an expected result?


    It's not a problem, but it does have a fix that is fairly easy.
    When setting Year, Month and Date, do it in reverse, ie. Date, Month, Year.
    That should fix the problem, as long as you don't try to setDate() to 31 in
    a month with only 30 days.

    If you get the date new Date(), and the date is 30 or 31, then you set the
    month to February, which has only 28 or 29 days, the system tries to figure
    it out before you set the date.

    So, simply change the order of how you set the information.

    Try doing the same thing on the 31st of January. Set the month to any
    other month with 30 days or fewer. Get the month. It will often come back
    as a different month than what you had set.

    I found out the hard way.

    Good luck, LJL
    LJL, Feb 2, 2004
    #4
  5. Re: Date object's getDay returns wrong date for Feb in any leapyear

    LJL <> writes:

    > It's not a problem, but it does have a fix that is fairly easy.


    .... although not very general.

    > When setting Year, Month and Date, do it in reverse, ie. Date, Month, Year.
    > That should fix the problem, as long as you don't try to setDate() to 31 in
    > a month with only 30 days.


    Exactly. No ordering will work all the time, so it is *far* better to
    set them all at the same time.
    myDate.setFullYear(2003,0,31);
    myDate.setFullYear(2004,1,29);
    (or setYear if you don't have setFullYear)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Feb 2, 2004
    #5
  6. JRS: In article <>, seen in
    news:comp.lang.javascript, Lasse Reichstein Nielsen <>
    posted at Mon, 2 Feb 2004 19:13:31 :-
    >LJL <> writes:
    >
    >> It's not a problem, but it does have a fix that is fairly easy.

    >
    >... although not very general.
    >
    >> When setting Year, Month and Date, do it in reverse, ie. Date, Month, Year.
    >> That should fix the problem, as long as you don't try to setDate() to 31 in
    >> a month with only 30 days.

    >
    >Exactly. No ordering will work all the time, so it is *far* better to
    >set them all at the same time.
    > myDate.setFullYear(2003,0,31);
    > myDate.setFullYear(2004,1,29);
    >(or setYear if you don't have setFullYear)


    On the Web, it is the reader, not the author, who has to have
    setFullYear.

    If there is doubt about setFullYear, note that ECMA-262 3rd Edn does not
    require setYear to accept M or D.

    One can always setDate(1), setMonth(M), setYear(Y), setDate(D).

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Feb 2, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hai Nguyen

    Date Validation Expression and Leap Year

    Hai Nguyen, Feb 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,311
    Hans Kesting
    Feb 27, 2004
  2. Wen
    Replies:
    4
    Views:
    461
    Simon Saunders
    Dec 12, 2003
  3. Protoman
    Replies:
    14
    Views:
    540
  4. Replies:
    8
    Views:
    491
    Howard
    Mar 30, 2007
  5. Veerle
    Replies:
    2
    Views:
    394
    niceguy
    Aug 20, 2003
Loading...

Share This Page