473,382 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
5 2317
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cp | last post by:
My background is C and Perl. I'm new to javascript, but not programming. I need to make two simple calendars with the next two months dates in them. I call them with the current month, as in: ...
5
by: David Woodward | last post by:
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()...
7
by: Esa | last post by:
function Validaattori_1viikko(source,args) { var star_tdate; var end_date; start_date = document.all("Laina_alku").value; end_date = document.all("Laina_loppu").value; var start_day; var...
2
by: mirza i | last post by:
thanks for the previous replies. here is the new question (i'm absolutely sure that this should be VERY easy for a good js coder) ok: from asp i call: <img src="pics/cal.gif"...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
10
by: Cylix | last post by:
I am going to define a date variable by specify a Date. How can I do so? I have tried below: var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006 but it shows "undefined-11-2006"...
7
by: erekose666 | last post by:
I need a java prog to do the following: Create class Date with the following capabilities: a) Output the date in multiple formats, such as: MM/DD/YYYY June 14, 2005 DDD YYYY b) Use...
12
by: jodishowers | last post by:
Greetings all. My 1st post here - hoping someone can help out. First I've tested this on FF 1.5.0.9, 2.x and Safari on OSX. here's the code that exhibits the behaviour: #case 1 - fails ...
4
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.