Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > a javascript problem

Reply
Thread Tools

a javascript problem

 
 
neo
Guest
Posts: n/a
 
      12-25-2004
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 == {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+".");


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      12-25-2004
On Sat, 25 Dec 2004 01:01:24 +0100, neo <> wrote:

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


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]

> 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 == {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+".");


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.
 
Reply With Quote
 
 
 
 
Jim Ley
Guest
Posts: n/a
 
      12-25-2004
On Sat, 25 Dec 2004 00:21:15 GMT, "Michael Winter"
<> wrote:

>On Sat, 25 Dec 2004 01:01:24 +0100, neo <> wrote:
>
>> 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.

>
>According to the ECMAScript specification, the getYear method is supposed
>to return the current year minus 1900.


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.


 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      12-25-2004
neo wrote:
> 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


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

Mick

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      12-25-2004
neo wrote:
> 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 == {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+".");


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','February','March','April',
'May','June','July','August',
'September','October','November','December'];

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



--
Rob
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      12-27-2004
JRS: In article <>, dated Sat, 25
Dec 2004 00:28:01, seen in news:comp.lang.javascript, Jim Ley
<> posted :

> best to just forget getYear if you
>can,


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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Ivo
Guest
Posts: n/a
 
      12-28-2004
"Dr John Stockton" wrote
> 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.


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.

> Anyone got ISO8601:2004 yet?


You 'll be the first to know.
Regards
Ivo


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      12-28-2004
Ivo said:
>
>"Dr John Stockton" wrote
>> 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.

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


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.

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


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.

 
Reply With Quote
 
Ivo
Guest
Posts: n/a
 
      12-28-2004
"Lee" wrote
> 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

I won't argue against anyone's narrowmindedness, 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


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      12-28-2004
Ivo said:
>
>"Lee" wrote
>> 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
>
>I won't argue against anyone's narrowmindedness, 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.


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 intercontinental
communication became easy and common, but now it's a source of confusion.
YMD eliminates that confusion because YDM is not common anywhere.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generated JavaScript vs. Manually Created JavaScript: Which one comes first? Nathan Sokalski ASP .Net 4 11-08-2007 07:24 AM
JavaScript or not JavaScript Mark Rae ASP .Net 36 09-09-2006 01:12 PM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
Javascript -> Applet: variable reset after method call from javascript?! mcdeveloper Java 1 06-13-2006 08:34 AM
tricky javascript problem requires javascript guru ! Andy Javascript 2 03-04-2004 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57