Date Month display problem

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

    Date Month display problem

    I have been using the javascript below on a web page since last August to
    show the "Site updated" month only minus a month, which has been very
    successful, but January is showing a "undefined 2004" message.
    Help much appreciated
    Jim

    <<<var m=new Array(13);var n=new Date()

    m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
    March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
    June";
    m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
    September";m[10]="Site updated October";m[11]="Site updated
    November";m[12]="Site updated December"
    document.write( m[n.getMonth()+0]+" "+n.getFullYear ())>>>


  • Jim Dabell

    #2
    Re: Date Month display problem

    Jim wrote:
    [color=blue]
    > I have been using the javascript below on a web page since last August to
    > show the "Site updated" month only minus a month, which has been very
    > successful, but January is showing a "undefined 2004" message.
    > Help much appreciated
    > Jim
    >
    > <<<var m=new Array(13);var n=new Date()
    >
    > m[1]="Site updated January";[/color]
    [snip]

    Arrays start from 0, not 1. You only need an array to hold 12 items, and
    you need to number them from 0. At the moment, you are trying to write out
    the string in m[0], which you haven't assigned to anything.

    --
    Jim Dabell

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Date Month display problem

      "Jim" <jim@internet.c om> writes:
      [color=blue]
      > I have been using the javascript below on a web page since last August to
      > show the "Site updated" month only minus a month, which has been very
      > successful,[/color]

      In making the users think you update the page frequently? :)
      [color=blue]
      > but January is showing a "undefined 2004" message.
      >
      > <<<var m=new Array(13);var n=new Date()
      >
      > m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
      > March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
      > June";
      > m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
      > September";m[10]="Site updated October";m[11]="Site updated
      > November";m[12]="Site updated December"
      > document.write( m[n.getMonth()+0]+" "+n.getFullYear ())>>>[/color]

      getMonth returns the month number starting with 0 for January and ending with
      11 for December. Your table has 1=>January, so it gives the previous month.
      That means that you can change
      m[12]="Site updated December"
      to
      m[0]="Site updated December"
      and it will work for the month. The year from getFullYear is the current
      one, so you will get December 2004 if that is all you do.
      I recommend the following instead:
      ---
      var monthName = [
      "January","Febr uary","March"," April","May","J une","July",
      "August","Septe mber","October" ,"November","De cember"];
      var now = new Date();
      now.setDate(1);
      now.setMonth(no w.getMonth()-1);
      document.write( "Site updated ",
      monthName(now.g etMonth()),
      " ",now.getFullYe ar());
      ---
      We set the date back by one month, so the getMonth and getFullYear
      refer to the same date. Setting the month to -1 will give December
      of the previous year.

      I set the date to the 1st to avoid problems when changing month.
      Otherwise, setting the month back by one on the 31th of March would
      give the 31th of February, which is normalized to give the 3rd of
      March.

      /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

      • McKirahan

        #4
        Re: Date Month display problem

        "Jim" <jim@internet.c om> wrote in message
        news:3ff86c71$0 $11175$cc9e4d1f @news.dial.pipe x.com...[color=blue]
        > I have been using the javascript below on a web page since last August to
        > show the "Site updated" month only minus a month, which has been very
        > successful, but January is showing a "undefined 2004" message.
        > Help much appreciated
        > Jim
        >
        > <<<var m=new Array(13);var n=new Date()
        >
        > m[1]="Site updated January";m[2]="Site updated February";m[3]="Site[/color]
        updated[color=blue]
        > March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site[/color]
        updated[color=blue]
        > June";
        > m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
        > September";m[10]="Site updated October";m[11]="Site updated
        > November";m[12]="Site updated December"
        > document.write( m[n.getMonth()+0]+" "+n.getFullYear ())>>>[/color]


        How about this instead?

        <html>
        <head>
        <title>SiteUpda ted.htm</title>
        <script language="javas cript" type="text/javascript">
        <!--
        var m = new Array(11);
        m[0] = "January";
        m[1] = "February";
        m[2] = "March";
        m[3] = "April";
        m[4] = "May";
        m[5] = "June";
        m[6] = "July";
        m[7] = "August";
        m[8] = "September" ;
        m[9] = "October";
        m[10] = "November";
        m[11] = "December"
        var n = new Date();
        document.write( "Site updated " + m[n.getMonth()] + " " + n.getFullYear() );
        // -->
        </script>
        </head>
        <body>
        </body>
        </html>


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Date Month display problem

          "McKirahan" <News@McKirahan .com> writes:
          [color=blue]
          > How about this instead?[/color]
          [color=blue]
          > var m = new Array(11);[/color]

          Why "11". You create an array with 11 empty slots, then put 12
          elements into it. There is no need for that 11 at all.
          [color=blue]
          > m[0] = "January";[/color]
          ....[color=blue]
          > m[11] = "December"[/color]

          It is shorter to just write:
          var m = new Array("January" ,"February", .... ,"December") ;
          (even shorter to write
          var m = ["January",...," December"];
          and it will only fail in browsers older than Netscape 4).

          /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

          • Jim

            #6
            Re: Date Month display problem

            Hi Lasse
            I like your recommended code but I can't get it to display at all.
            I am very new to java and may well be overlooking
            something simple.
            Regards
            Jim



            "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
            news:brpj798f.f sf@hotpop.com.. .[color=blue]
            > "Jim" <jim@internet.c om> writes:
            >[color=green]
            > > I have been using the javascript below on a web page since last August[/color][/color]
            to[color=blue][color=green]
            > > show the "Site updated" month only minus a month, which has been very
            > > successful,[/color]
            >
            > In making the users think you update the page frequently? :)
            >[color=green]
            > > but January is showing a "undefined 2004" message.
            > >
            > > <<<var m=new Array(13);var n=new Date()
            > >
            > > m[1]="Site updated January";m[2]="Site updated February";m[3]="Site[/color][/color]
            updated[color=blue][color=green]
            > > March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site[/color][/color]
            updated[color=blue][color=green]
            > > June";
            > > m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
            > > September";m[10]="Site updated October";m[11]="Site updated
            > > November";m[12]="Site updated December"
            > > document.write( m[n.getMonth()+0]+" "+n.getFullYear ())>>>[/color]
            >
            > getMonth returns the month number starting with 0 for January and ending[/color]
            with[color=blue]
            > 11 for December. Your table has 1=>January, so it gives the previous[/color]
            month.[color=blue]
            > That means that you can change
            > m[12]="Site updated December"
            > to
            > m[0]="Site updated December"
            > and it will work for the month. The year from getFullYear is the current
            > one, so you will get December 2004 if that is all you do.
            > I recommend the following instead:
            > ---
            > var monthName = [
            > "January","Febr uary","March"," April","May","J une","July",
            > "August","Septe mber","October" ,"November","De cember"];
            > var now = new Date();
            > now.setDate(1);
            > now.setMonth(no w.getMonth()-1);
            > document.write( "Site updated ",
            > monthName(now.g etMonth()),
            > " ",now.getFullYe ar());
            > ---
            > We set the date back by one month, so the getMonth and getFullYear
            > refer to the same date. Setting the month to -1 will give December
            > of the previous year.
            >
            > I set the date to the 1st to avoid problems when changing month.
            > Otherwise, setting the month back by one on the 31th of March would
            > give the 31th of February, which is normalized to give the 3rd of
            > March.
            >
            > /L
            > --
            > Lasse Reichstein Nielsen - lrn@hotpop.com
            > DHTML Death Colors:[/color]
            <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
            > 'Faith without judgement merely degrades the spirit divine.'[/color]


            Comment

            • Dr John Stockton

              #7
              Re: Date Month display problem

              JRS: In article <3ff86c71$0$111 75$cc9e4d1f@new s.dial.pipex.co m>, seen
              in news:comp.lang. javascript, Jim <jim@internet.c om> posted at Sun, 4
              Jan 2004 19:41:42 :-[color=blue]
              >I have been using the javascript below on a web page since last August to
              >show the "Site updated" month only minus a month, which has been very
              >successful, but January is showing a "undefined 2004" message.
              >Help much appreciated[/color]

              Have you considered the simple and honest approach of typing the correct
              date when the site is actually updated?

              As it is, your approach of giving an automated lie implies that nothing
              you write is trustworthy.

              Read the FAQ.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Date Month display problem

                "Jim" <jim@internet.c om> writes:
                [color=blue]
                > I like your recommended code but I can't get it to display at all.
                > I am very new to java and may well be overlooking
                > something simple.[/color]

                First thing: It's Javascript, not Java. Java is a completely different
                language.

                And please don't top post.

                There is a bug in the code, so no wonder it's not working. Here
                is a fixed version:

                <script type="text/javascript">
                var monthName = [
                "January","Febr uary","March"," April","May","J une","July",
                "August","Septe mber","October" ,"November","De cember"];
                var now = new Date();
                now.setMonth(no w.getMonth()-1,1);
                document.write( "Site updated ",
                monthName[now.getMonth()],
                " ",now.getFullYe ar());
                </script>

                (the square brackets after "monthName" had been replaced by parentheses)

                /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

                • Kien

                  #9
                  Re: Date Month display problem

                  How about the old document.lastMo difed ?

                  Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<O+pfDQE16 W+$EwXp@merlyn. demon.co.uk>...[color=blue]
                  > JRS: In article <3ff86c71$0$111 75$cc9e4d1f@new s.dial.pipex.co m>, seen
                  > in news:comp.lang. javascript, Jim <jim@internet.c om> posted at Sun, 4
                  > Jan 2004 19:41:42 :-[color=green]
                  > >I have been using the javascript below on a web page since last August to
                  > >show the "Site updated" month only minus a month, which has been very
                  > >successful, but January is showing a "undefined 2004" message.
                  > >Help much appreciated[/color]
                  >
                  > Have you considered the simple and honest approach of typing the correct
                  > date when the site is actually updated?
                  >
                  > As it is, your approach of giving an automated lie implies that nothing
                  > you write is trustworthy.
                  >
                  > Read the FAQ.[/color]

                  Comment

                  • Dr John Stockton

                    #10
                    Re: Date Month display problem

                    JRS: In article <166d5d19.04010 51812.fb4b4ba@p osting.google.c om>, seen
                    in news:comp.lang. javascript, Kien <caoxuankien@ho tmail.com> posted at
                    Mon, 5 Jan 2004 18:12:59 :-[color=blue]
                    >How about the old document.lastMo difed ?
                    >
                    >Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<O+pfDQE16 W+$Ew
                    >Xp@merlyn.demo n.co.uk>...[/color]
                    [color=blue][color=green]
                    >> Have you considered the simple and honest approach of typing the correct
                    >> date when the site is actually updated?
                    >>
                    >> As it is, your approach of giving an automated lie implies that nothing
                    >> you write is trustworthy.
                    >>
                    >> Read the FAQ.[/color][/color]


                    Please do not top-post.

                    Please do not over-quote.

                    Please read the FAQ.

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.

                    Comment

                    • paul

                      #11
                      Re: Date Month display problem


                      U¿ytkownik "Jim" <jim@internet.c om> napisa³ w wiadomo¶ci
                      news:3ff9b679$0 $10794$cc9e4d1f @news.dial.pipe x.com...[color=blue]
                      > Hi Lasse
                      > I like your recommended code but I can't get it to display at all.
                      > I am very new to java and may well be overlooking
                      > something simple.
                      > Regards
                      > Jim
                      >
                      >
                      >
                      > "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                      > news:brpj798f.f sf@hotpop.com.. .[color=green]
                      > > "Jim" <jim@internet.c om> writes:
                      > >[color=darkred]
                      > > > I have been using the javascript below on a web page since last August[/color][/color]
                      > to[color=green][color=darkred]
                      > > > show the "Site updated" month only minus a month, which has been very
                      > > > successful,[/color]
                      > >
                      > > In making the users think you update the page frequently? :)
                      > >[color=darkred]
                      > > > but January is showing a "undefined 2004" message.
                      > > >
                      > > > <<<var m=new Array(13);var n=new Date()
                      > > >
                      > > > m[1]="Site updated January";m[2]="Site updated February";m[3]="Site[/color][/color]
                      > updated[color=green][color=darkred]
                      > > > March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site[/color][/color]
                      > updated[color=green][color=darkred]
                      > > > June";
                      > > > m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
                      > > > September";m[10]="Site updated October";m[11]="Site updated
                      > > > November";m[12]="Site updated December"
                      > > > document.write( m[n.getMonth()+0]+" "+n.getFullYear ())>>>[/color]
                      > >
                      > > getMonth returns the month number starting with 0 for January and ending[/color]
                      > with[color=green]
                      > > 11 for December. Your table has 1=>January, so it gives the previous[/color]
                      > month.[color=green]
                      > > That means that you can change
                      > > m[12]="Site updated December"
                      > > to
                      > > m[0]="Site updated December"
                      > > and it will work for the month. The year from getFullYear is the current
                      > > one, so you will get December 2004 if that is all you do.
                      > > I recommend the following instead:
                      > > ---
                      > > var monthName = [
                      > > "January","Febr uary","March"," April","May","J une","July",
                      > > "August","Septe mber","October" ,"November","De cember"];
                      > > var now = new Date();
                      > > now.setDate(1);
                      > > now.setMonth(no w.getMonth()-1);
                      > > document.write( "Site updated ",
                      > > monthName(now.g etMonth()),
                      > > " ",now.getFullYe ar());
                      > > ---
                      > > We set the date back by one month, so the getMonth and getFullYear
                      > > refer to the same date. Setting the month to -1 will give December
                      > > of the previous year.
                      > >
                      > > I set the date to the 1st to avoid problems when changing month.
                      > > Otherwise, setting the month back by one on the 31th of March would
                      > > give the 31th of February, which is normalized to give the 3rd of
                      > > March.
                      > >
                      > > /L
                      > > --
                      > > Lasse Reichstein Nielsen - lrn@hotpop.com
                      > > DHTML Death Colors:[/color]
                      > <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=green]
                      > > 'Faith without judgement merely degrades the spirit divine.'[/color][/color]
                      Vamat CIE certification:
                      There is a bug in the code, so no wonder it's not working. Here
                      is a fixed version:

                      <script type="text/javascript">
                      var monthName = [
                      "January","Febr uary","March"," April","May","J une","July",
                      "August","Septe mber","October" ,"November","De cember"];
                      var now = new Date();
                      now.setMonth(no w.getMonth()-1,1);
                      document.write( "Site updated ",
                      monthName[now.getMonth()],
                      " ",now.getFullYe ar());
                      </script>
                      Lasse Reichstein Nielsen - lrn@hotpop.com


                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: Date Month display problem

                        Lasse Reichstein Nielsen wrote:
                        [color=blue]
                        > "McKirahan" <News@McKirahan .com> writes:[color=green]
                        >> How about this instead?
                        >>
                        >> var m = new Array(11);[/color]
                        >
                        > Why "11". You create an array with 11 empty slots, [...][/color]

                        Or an array with 1 non-empty slot containing a literal 11.
                        Depends on the ECMAScript implementation, so array literals
                        are the better way if the first element yields `true' for
                        !isNaN(...).


                        PointedEars

                        Comment

                        Working...