Trouble With Date Code

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

    Trouble With Date Code

    Can anyone here help me with this...

    I have a date code to display the date 7 days in the future— check it
    out here : http://hidefsounds.com/date.html

    The problem is that the day of the month doesn't reset when it's past
    30/31 days... So currently it shows:

    "May 36, 2008."

    Any help would be appreciated. Thanks in advance.
  • Lasse Reichstein Nielsen

    #2
    Re: Trouble With Date Code

    sixstringsk <thekleincollec tion@yahoo.comw rites:
    I have a date code to display the date 7 days in the future— check it
    out here : http://hidefsounds.com/date.html
    >
    The problem is that the day of the month doesn't reset when it's past
    30/31 days... So currently it shows:
    >
    "May 36, 2008."
    Use a Date object when you work with dates. It prevents you from
    creating non-existing dates by wrapping, e.g., 36th of May into
    5th of June.

    var d = new Date(); // today
    d.setDate(d.get Date()+7); // 7 days later
    // format as string before displaying

    /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

    • Dr J R Stockton

      #3
      Re: Trouble With Date Code

      In comp.lang.javas cript message <a1b603ad-f021-4c69-b0ee-57b25e0dd3ae@27
      g2000hsf.google groups.com>, Thu, 29 May 2008 20:43:48, sixstringsk
      <thekleincollec tion@yahoo.comp osted:
      >Can anyone here help me with this...
      >
      >I have a date code to display the date 7 days in the future— check it
      >out here : http://hidefsounds.com/date.html
      >
      >The problem is that the day of the month doesn't reset when it's past
      >30/31 days... So currently it shows:
      >
      >"May 36, 2008."
      >
      >Any help would be appreciated. Thanks in advance.

      Code which is that short should be posted to the newsgroup, for easier
      handling.

      <QUOTE>
      <script language="JavaS cript1.2">
      // ^^^^^^^^^^^^^^^ Deprecated.
      // text/javascript preferred, or nothing.
      <!-- Begin
      // superfluous
      var months=new Array(13);
      months[1]="January";
      months[2]="February";
      months[3]="March";
      months[4]="April";
      months[5]="May";
      months[6]="June";
      months[7]="July";
      months[8]="August";
      months[9]="September" ;
      months[10]="October";
      months[11]="November";
      months[12]="December";

      // var months = ["January", ... "December"] better, and zero-based

      var time=new Date();
      var lmonth=months[time.getMonth() + 1];
      // then no need to add 1
      var date=time.getDa te() +7;
      var year=time.getYe ar();
      if (year < 2000) // Y2K Fix, Isaac Powell
      year = year + 1900; // http://onyx.idbsu.edu/~ipowell
      // ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ - cannot find
      // time.getFullYea r() is better
      document.write( "" + lmonth + " ");
      // ^^^^ superfluous
      document.write( date + ", " + year + "");
      // End -->
      // superfluous

      </script>
      </QUOTE>

      Obviously the month will over-range, as you have done nothing to make it
      do otherwise.

      You should follow Lasse's advice, but ignore the erroneous signature.

      Otherwise

      It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

      --
      (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
      news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
      <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...