Changing Date.setMonth() behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    Changing Date.setMonth() behavior

    Is there a way, besides writing another method, to make
    Date.setMonth() do something more useful than nothing when the month
    in question creates an invalid date? If I try

    d=new Date();
    d.setMonth( 1 );

    today, I'd really like to get some kind of an error rather than silent
    failure.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Michael Winter

    #2
    Re: Changing Date.setMonth() behavior

    On 31/03/2005 01:26, Christopher Benson-Manica wrote:
    [color=blue]
    > Is there a way, besides writing another method, to make
    > Date.setMonth() do something more useful than nothing when the month
    > in question creates an invalid date? If I try
    >
    > d=new Date();
    > d.setMonth( 1 );
    >
    > today, I'd really like to get some kind of an error rather than silent
    > failure.[/color]

    But you don't get silent failure. The date is wrapped so that it
    becomes valid. I think that's pretty useful, so what would be more
    useful in your opinion?

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Fred Oz

      #3
      Re: Changing Date.setMonth() behavior

      Christopher Benson-Manica wrote:[color=blue]
      > Is there a way, besides writing another method, to make
      > Date.setMonth() do something more useful than nothing when the month
      > in question creates an invalid date? If I try
      >
      > d=new Date();
      > d.setMonth( 1 );
      >
      > today, I'd really like to get some kind of an error rather than silent
      > failure.
      >[/color]

      As Mike said, there is no failure. The expected behaviour is that if
      no date is specified when setMonth() is called, the value returned
      from getUTCDate() is used.

      If the date is 2005-mar-31 and you use setMonth(1) to create a date
      of 2005-feb-31, the expected behaviour is that parameters are updated
      in accordance with the ECMA 262 spec and 2005-03-03 is returned.

      E.g.

      var n = new Date();
      n.setMonth(n.ge tMonth + 15)

      will effectively add 1 to the year and 3 to the months. And given
      that for today (2005-03-31) that will create a date of 31 June, the
      returned date is 01 July 2005

      So while the result you get it not what you expect, it is not
      'failure' and is precisely according to the specification.

      You need to determine your requirements - maybe you are trying to
      find the date of the last day of the month for a month x months
      previous or hence? In that case, the most efficient method does not
      require a date object at all, just numbers for month and year.


      --
      Fred

      Comment

      • Fred Oz

        #4
        Re: Changing Date.setMonth() behavior

        Fred Oz wrote:
        [...][color=blue]
        > E.g.
        >
        > var n = new Date();
        > n.setMonth(n.ge tMonth + 15)
        >
        > will effectively add 1 to the year and 3 to the months. And given
        > that for today (2005-03-31) that will create a date of 31 June, the
        > returned date is 01 July 2005[/color]

        mumble mumble... 01 July *2006* .... mumble mumble ....

        --
        Fred

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Changing Date.setMonth() behavior

          Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
          [color=blue]
          > If I try
          >
          > d=new Date();
          > d.setMonth( 1 );
          >
          > today, I'd really like to get some kind of an error rather than silent
          > failure.[/color]

          Sorry, that's not how Date works. You will have to create your own
          method that checks whether the setting succeeded:

          function setMonth(date, month) {
          date.setMonth(m onth);
          if (date.getMonth( ) != month) {
          throw "invalid date for month";
          // notice: date has already been updated.
          }
          }


          The change to the date object is not a silent failure. It is deliberatly
          treating the 31th of Februaray this year as an alternative way to write
          the 3rd of March.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: Changing Date.setMonth() behavior

            JRS: In article <1x9wy4ao.fsf@h otpop.com>, dated Thu, 31 Mar 2005
            03:47:43, seen in news:comp.lang. javascript, Lasse Reichstein Nielsen
            <lrn@hotpop.com > posted :[color=blue]
            >Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
            >[color=green]
            >> If I try
            >>
            >> d=new Date();
            >> d.setMonth( 1 );
            >>
            >> today, I'd really like to get some kind of an error rather than silent
            >> failure.[/color][/color]

            Read the FAQ on dates; see sig.

            Note that you can do d.setMonth(1, 1).

            [color=blue]
            >Sorry, that's not how Date works. You will have to create your own
            >method that checks whether the setting succeeded:
            >
            >function setMonth(date, month) {
            > date.setMonth(m onth);
            > if (date.getMonth( ) != month) {
            > throw "invalid date for month";
            > // notice: date has already been updated.
            > }
            >}[/color]


            That fails if the month being set is outside the range 0 to 11 and the
            target month has sufficient days. I have preferred to get the initial
            and final dates, and to take action if they differ.

            function AlterMonth(DObj , By, Back) { // Back is boolean
            with (DObj) { var Xd = getDate() ; setMonth(getMon th() + By)
            if (Xd != getDate()) setDate(Number( Back)) // 0 or 1, as needed
            } } // js-date0.htm

            With that and yours, try going back 5 months from May 31st.

            As you see, my code presumes that, rather than failure, either the last
            day of the inadequate month or the first day of its successor should be
            returned.

            I expect it would be faster in UTC.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            Working...