f



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


0
neo
12/25/2004 12:01:24 AM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

12 Replies
741 Views

Similar Articles

[PageSpeed] 54

On Sat, 25 Dec 2004 01:01:24 +0100, neo <tina69@net4u.hr> 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 == 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+".");

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.
0
Michael
12/25/2004 12:21:15 AM
On Sat, 25 Dec 2004 00:21:15 GMT, "Michael Winter"
<M.Winter@blueyonder.co.invalid> wrote:

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


0
jim
12/25/2004 12:28:01 AM
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

0
Mick
12/25/2004 12:33:14 AM
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 == 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+"."); 

  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
0
RobG
12/25/2004 6:29:20 AM
JRS:  In article <41ccb3d3.23479441@news.individual.net>, dated Sat, 25
Dec 2004 00:28:01, seen in news:comp.lang.javascript, Jim Ley
<jim@jibbering.com> 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.
0
Dr
12/27/2004 10:48:42 PM
"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


0
Ivo
12/28/2004 2:36:54 AM
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.

0
Lee
12/28/2004 3:10:49 AM
"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


0
Ivo
12/28/2004 4:56:53 AM
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.

0
Lee
12/28/2004 12:14:17 PM
JRS:  In article <41d0c492$0$20460$d5255a0c@news.wanadoo.nl>, dated Tue,
28 Dec 2004 03:36:54, seen in news:comp.lang.javascript, Ivo
<no@thank.you> posted :
>"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.

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


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

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

>absolutely needless, mind-boggling, unnatural and cold, and indeed invented
>just to confuse the ordinary world, the real world outside the electronic
>one.

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.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
 Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
0
Dr
12/28/2004 12:41:45 PM
JRS:  In article <cqrimp012o@drn.newsguy.com>, dated Tue, 28 Dec 2004
04:14:17, seen in news:comp.lang.javascript, Lee
<REM0VElbspamtrap@cox.net> posted :

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

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.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
 Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
0
Dr
12/28/2004 8:39:44 PM
In article <LT8OY5PKFJ0BFwvW@merlyn.demon.co.uk>, Dr John Stockton
<spam@merlyn.demon.co.uk> writes

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

For "technically-civilised" read punched-card control freak.

  John
-- 
John Harris
0
John
12/29/2004 10:16:04 AM
Reply: