473,809 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 23 '05 #1
5 2337
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.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
Jul 23 '05 #3
Fred Oz wrote:
[...]
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


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

--
Fred
Jul 23 '05 #4
Christopher Benson-Manica <at***@nospam.c yberspace.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(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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5
JRS: In article <1x**********@h otpop.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.c yberspace.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(m onth);
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(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.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1536
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: makeCalNav('2003', '11'); function makeCalNav(yy,mm) { var code = '';
5
2774
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() oDate.setYear(2004) oDate.setMonth(1) oDate.setDate(1) iDay = oDate.getDay()
7
2176
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 end_day; var start_month; var end_month;
2
1583
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" onclick="show_calendar_det('document.invdetdet.dx1',
12
29483
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 follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
10
1572
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" and I found IE6 cannot using: var d=new Date; d=d.setFullYear(2006)
7
3956
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 overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third...
12
2692
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 var date1 = new Date(); date1.setYear(2007); date1.setMonth(3);
4
3380
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 the comments and clicks the save button then the date and the date will be stored in the mysql database. This is working fine. But my problem is when, after the user had made an entry the date in the calendar for which an entry has made should be...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.