By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,629 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,629 IT Pros & Developers. It's quick & easy.

Changing Date.setMonth() behavior

P: n/a
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)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 31/03/2005 01:26, Christopher Benson-Manica wrote:
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.


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.
Jul 23 '05 #2

P: n/a
Christopher Benson-Manica wrote:
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.


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.
--
Fred
Jul 23 '05 #3

P: n/a
Fred Oz wrote:
[...]
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


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

--
Fred
Jul 23 '05 #4

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
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.


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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5

P: n/a
JRS: In article <1x**********@hotpop.com>, dated Thu, 31 Mar 2005
03:47:43, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com> posted :
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
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.

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:

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

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.

--
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.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.