Date() question

C

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?
 
L

Lasse Reichstein Nielsen

cp said:
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.
}
}
 
C

cp

Lasse Reichstein Nielsen said:
Why pass numbers as strings?

Noted. Thanks.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top