a javascript problem

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

    a javascript problem

    In IE this javascript cod for Date works perfectly, for a eksample:
    25.12.2004. but in other Internet Viewers like Firefox or Natescape, it
    writes 25.12.104.
    Can enybody help me. And Happy Christmas to you all!

    here is the code

    myvar = new Date();
    Month = (myvar.getMonth () + 1)
    Year = (myvar.getYear( ))
    if (Month == 1) {WordMonth = "1";}
    if (Month == 2) {WordMonth = "2";}
    if (Month == 3) {WordMonth = "3";}
    if (Month == 4) {WordMonth = "4";}
    if (Month == 5) {WordMonth = "5";}
    if (Month == 6) {WordMonth = "6";}
    if (Month == 7) {WordMonth = "7";}
    if (Month == 8) {WordMonth = "8";}
    if (Month == 9) {WordMonth = "9";}
    if (Month == 10) {WordMonth = "10";}
    if (Month == 11) {WordMonth = "11";}
    if (Month == 12) {WordMonth = "12";}
    document.write( myvar.getDate() +". "+WordMonth +". "+Year+".") ;


  • Michael Winter

    #2
    Re: a javascript problem

    On Sat, 25 Dec 2004 01:01:24 +0100, neo <tina69@net4u.h r> wrote:
    [color=blue]
    > In IE this javascript cod for Date works perfectly, for a eksample:
    > 25.12.2004. but in other Internet Viewers like Firefox or Natescape, it
    > writes 25.12.104.[/color]

    According to the ECMAScript specification, the getYear method is supposed
    to return the current year minus 1900. IE doesn't follow this. Use the
    getFullYear method instead.

    [snip]
    [color=blue]
    > myvar = new Date();
    > Month = (myvar.getMonth () + 1)
    > Year = (myvar.getYear( ))
    > if (Month == 1) {WordMonth = "1";}
    > if (Month == 2) {WordMonth = "2";}
    > if (Month == 3) {WordMonth = "3";}
    > if (Month == 4) {WordMonth = "4";}
    > if (Month == 5) {WordMonth = "5";}
    > if (Month == 6) {WordMonth = "6";}
    > if (Month == 7) {WordMonth = "7";}
    > if (Month == 8) {WordMonth = "8";}
    > if (Month == 9) {WordMonth = "9";}
    > if (Month == 10) {WordMonth = "10";}
    > if (Month == 11) {WordMonth = "11";}
    > if (Month == 12) {WordMonth = "12";}
    > document.write( myvar.getDate() +". "+WordMonth +". "+Year+".") ;[/color]

    This would be much simpler as:

    var now = new Date();
    document.write( now.getDate() + '.' + (now.getMonth() + 1) + '.'
    + now.getFullYear ());

    The conversion to string will occur automatically. However, you should
    avoid ambiguous formats like dd/mm/yyyy if you expect visitors from other
    countries.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Jim Ley

      #3
      Re: a javascript problem

      On Sat, 25 Dec 2004 00:21:15 GMT, "Michael Winter"
      <M.Winter@bluey onder.co.invali d> wrote:
      [color=blue]
      >On Sat, 25 Dec 2004 01:01:24 +0100, neo <tina69@net4u.h r> wrote:
      >[color=green]
      >> In IE this javascript cod for Date works perfectly, for a eksample:
      >> 25.12.2004. but in other Internet Viewers like Firefox or Natescape, it
      >> writes 25.12.104.[/color]
      >
      >According to the ECMAScript specification, the getYear method is supposed
      >to return the current year minus 1900.[/color]

      That's not a spec though, it's just a suggestion if it is implemented,
      there's no actual requirement too, best to just forget getYear if you
      can, if not see the FAQ for links to Dr John Stocktons pages.

      Jim.


      Comment

      • Mick White

        #4
        Re: a javascript problem

        neo wrote:[color=blue]
        > In IE this javascript cod for Date works perfectly, for a eksample:
        > 25.12.2004. but in other Internet Viewers like Firefox or Natescape, it
        > writes 25.12.104.
        > Can enybody help me. And Happy Christmas to you all!
        >
        > here is the code[/color]

        d=new Date();
        document.write( d.getDate()+"." +(d.getMonth()+ 1)+"."+d.getFul lYear());

        Mick

        Comment

        • RobG

          #5
          Re: a javascript problem

          neo wrote:[color=blue]
          > In IE this javascript cod for Date works perfectly, for a eksample:
          > 25.12.2004. but in other Internet Viewers like Firefox or Natescape, it
          > writes 25.12.104.
          > Can enybody help me. And Happy Christmas to you all!
          >
          > here is the code
          >
          > myvar = new Date();
          > Month = (myvar.getMonth () + 1)
          > Year = (myvar.getYear( ))
          > if (Month == 1) {WordMonth = "1";}
          > if (Month == 2) {WordMonth = "2";}
          > if (Month == 3) {WordMonth = "3";}
          > if (Month == 4) {WordMonth = "4";}
          > if (Month == 5) {WordMonth = "5";}
          > if (Month == 6) {WordMonth = "6";}
          > if (Month == 7) {WordMonth = "7";}
          > if (Month == 8) {WordMonth = "8";}
          > if (Month == 9) {WordMonth = "9";}
          > if (Month == 10) {WordMonth = "10";}
          > if (Month == 11) {WordMonth = "11";}
          > if (Month == 12) {WordMonth = "12";}
          > document.write( myvar.getDate() +". "+WordMonth +". "+Year+".") ;[/color]

          Given that "WordMonth" looks like you are going to replace the number
          with the word, here's a simple way to do it without all those ifs:

          var monthsA = ['January','Febr uary','March',' April',
          'May','June','J uly','August',
          'September','Oc tober','Novembe r','December'];

          function showDate() {
          d=new Date();
          alert(d.getDate ()
          + " " + monthsA[d.getMonth()]
          + " " + d.getFullYear() );
          }



          --
          Rob

          Comment

          • Dr John Stockton

            #6
            Re: a javascript problem

            JRS: In article <41ccb3d3.23479 441@news.indivi dual.net>, dated Sat, 25
            Dec 2004 00:28:01, seen in news:comp.lang. javascript, Jim Ley
            <jim@jibbering. com> posted :
            [color=blue]
            > best to just forget getYear if you
            >can,[/color]

            For maximum compatibility, forget the method getFullYear, since it does
            not always exist, and use getYear plus thought to develop a function for
            calculating the Full Year. Such a function can be found /via/ the FAQ.
            ISTM hardly worth replacing, or conditionally providing, getFullYear,
            since the function suffices; but purists will wish to do so.

            The OP should have read the FAQ; IMHO, the only justification for using
            DMY rather than YMD is to confuse or annoy the MDY brigade (or to
            satisfy management).

            The OP should also use a Leading Zero function (/loc cit/), since using
            single-digit day & month in all-numeric dates is deprecated throughout
            the technically-civilised world.

            Anyone got ISO8601:2004 yet?

            Regards,

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of 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

            • Ivo

              #7
              Re: a javascript problem

              "Dr John Stockton" wrote[color=blue]
              > The OP should have read the FAQ; IMHO, the only justification for using
              > DMY rather than YMD is to confuse or annoy the MDY brigade (or to
              > satisfy management).
              >
              > The OP should also use a Leading Zero function (/loc cit/), since using
              > single-digit day & month in all-numeric dates is deprecated throughout
              > the technically-civilised world.[/color]

              Two remarks which strike me as alarmingly narrow-minded, I regret to say.
              DMY is the only thing that my boss and my parents understand, are willing to
              understand even. It was good for centuries, and it 's good enough for them.
              They detest YMD and most other date forms as American aberrations,
              absolutely needless, mind-boggling, unnatural and cold, and indeed invented
              just to confuse the ordinary world, the real world outside the electronic
              one.

              And although I consider myself part of the technically-civilised world, I
              usually make an express point of removing leading zeros. Even in a situation
              of dates listed in a table column or so, replacing them with spaces to
              maintain the vertical cohesion is absolutely worth the effort when the point
              is getting information across to a general audience. Plenty of people will
              still translate 7 into July and 4 into April before putting them in order.
              [color=blue]
              > Anyone got ISO8601:2004 yet?[/color]

              You 'll be the first to know.
              Regards
              Ivo


              Comment

              • Lee

                #8
                Re: a javascript problem

                Ivo said:[color=blue]
                >
                >"Dr John Stockton" wrote[color=green]
                >> The OP should have read the FAQ; IMHO, the only justification for using
                >> DMY rather than YMD is to confuse or annoy the MDY brigade (or to
                >> satisfy management).
                >>
                >> The OP should also use a Leading Zero function (/loc cit/), since using
                >> single-digit day & month in all-numeric dates is deprecated throughout
                >> the technically-civilised world.[/color]
                >
                >Two remarks which strike me as alarmingly narrow-minded, I regret to say.
                >DMY is the only thing that my boss and my parents understand, are willing to
                >understand even. It was good for centuries, and it 's good enough for them.
                >They detest YMD and most other date forms as American aberrations,
                >absolutely needless, mind-boggling, unnatural and cold, and indeed invented
                >just to confuse the ordinary world, the real world outside the electronic
                >one.[/color]

                But they're demonstrably wrong. YMD is the closest we've got to an unambiguous
                date format. They are the narrow-minded ones who need to reconsider. Coddling
                them does nobody any good. It takes nothing more than willingness to accept
                change to understand YMD, particularly for those who are used to DMY, rather
                thatn MDY.
                [color=blue]
                >And although I consider myself part of the technically-civilised world, I
                >usually make an express point of removing leading zeros. Even in a situation
                >of dates listed in a table column or so, replacing them with spaces to
                >maintain the vertical cohesion is absolutely worth the effort when the point
                >is getting information across to a general audience.[/color]

                Are you saying that you believe not only that a majority prefer to see
                "28/ 2/2004" rather than "28/02/2004", but also that significantly more
                people can read the former than the latter?

                That's a pretty astounding cultural difference from what I've experienced.

                Comment

                • Ivo

                  #9
                  Re: a javascript problem

                  "Lee" wrote[color=blue]
                  > But they're demonstrably wrong. YMD is the closest we've got to an[/color]
                  unambiguous[color=blue]
                  > date format. They are the narrow-minded ones who need to reconsider.[/color]
                  Coddling

                  I won't argue against anyone's narrowmindednes s, but how can you mean
                  "wrong"? For centuries, we have been telling each other that the earth was
                  flat. Then science advanced and we learned we were wrong. The earth is
                  round. During those same centuries, we have been talking in DMY format,
                  except perhaps on some remote islands. Then science advanced and we found it
                  easier to start talking YMD. But all the other formats did not suddenly turn
                  out to be lies.
                  Talking about the use of date formats in everyday old fashioned
                  human-to-human language, as very much opposed to its use in communications
                  with and between computers. It 's just a format, like language, just a
                  couple of commonly agreed upon conventions, subject to discussion
                  constantly. YMD was "wrong" until we decided to see it as a logical format,
                  convenient when sorting etc. Really, that was just a few years ago.
                  The matter seems highly relevant here among web authors. It seems the time
                  is near when we start pronouncing leading zeros.

                  Ivo


                  Comment

                  • Lee

                    #10
                    Re: a javascript problem

                    Ivo said:[color=blue]
                    >
                    >"Lee" wrote[color=green]
                    >> But they're demonstrably wrong. YMD is the closest we've got to an[/color]
                    >unambiguous[color=green]
                    >> date format. They are the narrow-minded ones who need to reconsider.[/color]
                    >Coddling
                    >
                    >I won't argue against anyone's narrowmindednes s, but how can you mean
                    >"wrong"? For centuries, we have been telling each other that the earth was
                    >flat. Then science advanced and we learned we were wrong. The earth is
                    >round. During those same centuries, we have been talking in DMY format,
                    >except perhaps on some remote islands.[/color]

                    Here lies your mistake. For centuries, *some* people have written DMY, and
                    some have written MDY. In the United States and possibly elsewhere, people
                    expect to see MDY. That wasn't much of a problem before intercontinenta l
                    communication became easy and common, but now it's a source of confusion.
                    YMD eliminates that confusion because YDM is not common anywhere.

                    Comment

                    • Dr John Stockton

                      #11
                      Re: a javascript problem

                      JRS: In article <41d0c492$0$204 60$d5255a0c@new s.wanadoo.nl>, dated Tue,
                      28 Dec 2004 03:36:54, seen in news:comp.lang. javascript, Ivo
                      <no@thank.you > posted :[color=blue]
                      >"Dr John Stockton" wrote[color=green]
                      >> The OP should have read the FAQ; IMHO, the only justification for using
                      >> DMY rather than YMD is to confuse or annoy the MDY brigade (or to
                      >> satisfy management).
                      >>
                      >> The OP should also use a Leading Zero function (/loc cit/), since using
                      >> single-digit day & month in all-numeric dates is deprecated throughout
                      >> the technically-civilised world.[/color]
                      >
                      >Two remarks which strike me as alarmingly narrow-minded, I regret to say.[/color]

                      You appear to have responded without considering the qualification
                      "technicall y-civilised". That, usually but not always, excludes
                      employers and ancestors.

                      [color=blue]
                      >DMY is the only thing that my boss and my parents understand, are willing to
                      >understand even. It was good for centuries, and it 's good enough for them.
                      >They detest YMD and most other date forms as American aberrations,[/color]

                      Criticism of US practices is so widely justifiable that there is really
                      no need to blame them for something that they obstinately avoid (apart
                      from, AIUI, making it a Federal Standard).
                      [color=blue]
                      >absolutely needless, mind-boggling, unnatural and cold, and indeed invented
                      >just to confuse the ordinary world, the real world outside the electronic
                      >one.[/color]

                      YMD is, AIUI, the form customarily used in the Far East; and that means
                      by rather more people than there are in the USA.


                      Never choose either of DMY or MDY for the obtuse (except for the sort of
                      American that will never have friendly or business contact with
                      elsewhere), since it will mislead those of the obtuse who favour the
                      other one of them. There, use D+ Month YYYY in any order, where Month
                      is the month-name or its TLA.

                      OTOH, I don't know whether, in the EU, there is yet an instance of
                      linguistically-preferred month-TLAs where the same TLA refers to
                      different months; but, with the number of possibilities, I'd not be
                      surprised.


                      It is better to give material in an unambiguous, comprehensible form
                      than in a form which may only apparently be in the preferred form.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                      Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                      PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                      Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                      Comment

                      • Dr John Stockton

                        #12
                        Re: a javascript problem

                        JRS: In article <cqrimp012o@drn .newsguy.com>, dated Tue, 28 Dec 2004
                        04:14:17, seen in news:comp.lang. javascript, Lee
                        <REM0VElbspamtr ap@cox.net> posted :
                        [color=blue]
                        >Here lies your mistake. For centuries, *some* people have written DMY, and
                        >some have written MDY. In the United States and possibly elsewhere, people
                        >expect to see MDY. That wasn't much of a problem before intercontinenta l
                        >communicatio n became easy and common, but now it's a source of confusion.
                        >YMD eliminates that confusion because YDM is not common anywhere.[/color]

                        Understating one's case is not good practice.

                        AFAICS, numeric YDM is totally unknown in the wild, and can only be seen
                        as a result of someone's brain fatigue while editing code.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                        Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                        PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                        Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                        Comment

                        • John G Harris

                          #13
                          Re: a javascript problem

                          In article <LT8OY5PKFJ0BFw vW@merlyn.demon .co.uk>, Dr John Stockton
                          <spam@merlyn.de mon.co.uk> writes

                          <snip>[color=blue]
                          >The OP should also use a Leading Zero function (/loc cit/), since using
                          >single-digit day & month in all-numeric dates is deprecated throughout
                          >the technically-civilised world.[/color]

                          For "technicall y-civilised" read punched-card control freak.

                          John
                          --
                          John Harris

                          Comment

                          Working...