470,591 Members | 1,770 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Date() question

cp
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 = '';
// make a two-month calendar starting with next month
for ( i=1; i<3; i++ ){
cal = initCalendar( yy, mm + i);
code += drawCal( cal );
}
return code;
}

function initCalendar( year, month ) {
var Calendar = new Date(year, month, 1);
return Calendar;
}

In Perl, passing 2003, 12 to a date routine would be a value out of
range. Javascript IE and Mozilla for Mac seem to correctly increment
the year and initialize the calendar for jan. 2004 and feb. 2004, Is
this as it should be?

--
cp
Jul 20 '05 #1
2 1472
cp <cp****@nospam.pryce.net> writes:
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');
Why pass numbers as strings?
function initCalendar( year, month ) {
var Calendar = new Date(year, month, 1);
return Calendar;
}

In Perl, passing 2003, 12 to a date routine would be a value out of
range. Javascript IE and Mozilla for Mac seem to correctly increment
the year and initialize the calendar for jan. 2004 and feb. 2004, Is
this as it should be?


Yes.

Btw, notice that the Javascript/ECMAScript Date object counts months
between 0 and 11.

When "overflowing" the Date object, the date is normalized, so the
32nd of December 2003 is normalized to the 1st of January 2004. This
behavior is described in detail in the ECMAScript standard (ECMA 262).

That makes it very easy to find the next month or next day.

Example:
---
function makeCalNav(yy,mm) {
var date = new Date(yy,mm-1,1); // -1 because Date months are 0-11
for (i=0;i<2;i++) {
drawCal(date);
date.setMonth(date.getMonth()+1); // next month, always.
}
}
---

/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 20 '05 #2
cp
In article <u1**********@hotpop.com>, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
makeCalNav('2003', '11');
Why pass numbers as strings?


Noted. Thanks.
function initCalendar( year, month ) {
var Calendar = new Date(year, month, 1);
return Calendar;
}

In Perl, passing 2003, 12 to a date routine would be a value out of
range. Javascript IE and Mozilla for Mac seem to correctly increment
the year and initialize the calendar for jan. 2004 and feb. 2004, Is
this as it should be?
Yes.

Btw, notice that the Javascript/ECMAScript Date object counts months
between 0 and 11.


yes, hence the note that passing 2003, 12 would be invalid in Perl.
There is no 12 month in a 0-11 range.

[snip] date.setMonth(date.getMonth()+1); // next month, always.
}
}


The answer that I was looking for. Thank you very much.

--
cp
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mirza i | last post: by
12 posts views Thread by Steve Elliott | last post: by
5 posts views Thread by Kermit Piper | last post: by
9 posts views Thread by Martin | last post: by
6 posts views Thread by Geoff Cox | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.