Date() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cp

    #1

    Date() question

    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('200 3', '11');

    function makeCalNav(yy,m m) {
    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
  • Lasse Reichstein Nielsen

    #2
    Re: Date() question

    cp <cpryce@nospam. pryce.net> writes:
    [color=blue]
    > 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('200 3', '11');[/color]

    Why pass numbers as strings?
    [color=blue]
    > 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?[/color]

    Yes.

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

    When "overflowin g" 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,m m) {
    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(d ate.getMonth()+ 1); // next month, always.
    }
    }
    ---

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • cp

      #3
      Re: Date() question

      In article <u12nacm5.fsf@h otpop.com>, Lasse Reichstein Nielsen
      <lrn@hotpop.com > wrote:
      [color=blue][color=green]
      > > makeCalNav('200 3', '11');[/color]
      >
      > Why pass numbers as strings?[/color]

      Noted. Thanks.[color=blue]
      >[color=green]
      > > 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?[/color]
      >
      > Yes.
      >
      > Btw, notice that the Javascript/ECMAScript Date object counts months
      > between 0 and 11.[/color]

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

      [snip][color=blue]
      > date.setMonth(d ate.getMonth()+ 1); // next month, always.
      > }
      > }[/color]

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

      --
      cp

      Comment

      Working...