Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / March 2005



Tip: Looking for answers? Try searching our database.

Changing Date.setMonth() behavior

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christopher Benson-Manica - 31 Mar 2005 01:26 GMT
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.

Signature

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

Michael Winter - 31 Mar 2005 01:43 GMT
> Is there a way, besides writing another method, to make
> Date.setMonth() do something more useful than nothing when the month
[quoted text clipped - 5 lines]
> today, I'd really like to get some kind of an error rather than silent
> failure.

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

Signature

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

Fred Oz - 31 Mar 2005 02:42 GMT
> Is there a way, besides writing another method, to make
> Date.setMonth() do something more useful than nothing when the month
[quoted text clipped - 5 lines]
> today, I'd really like to get some kind of an error rather than silent
> failure.

 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.getMonth + 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.

Signature

Fred

Fred Oz - 31 Mar 2005 02:44 GMT
[...]
>  E.g.
>
[quoted text clipped - 4 lines]
>   that for today (2005-03-31) that will create a date of 31 June, the
>   returned date is 01 July 2005

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

Signature

Fred

Lasse Reichstein Nielsen - 31 Mar 2005 02:47 GMT
> If I try
>
[quoted text clipped - 3 lines]
> today, I'd really like to get some kind of an error rather than silent
> failure.

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(month);
 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
Signature

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

Dr John Stockton - 31 Mar 2005 21:47 GMT
JRS:  In article <1x9wy4ao.fsf@hotpop.com>, dated Thu, 31 Mar 2005
03:47:43, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lrn@hotpop.com> posted :

>> If I try
>>
[quoted text clipped - 3 lines]
>> today, I'd really like to get some kind of an error rather than silent
>> failure.

Read the FAQ on dates; see sig.

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

>Sorry, that's not how Date works. You will have to create your own
>method that checks whether the setting succeeded:
[quoted text clipped - 6 lines]
>  }
>}

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(getMonth() + 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.

Signature

© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
<URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of 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.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.