a javascript problem

N

neo

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

Michael Winter

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
 
J

Jim Ley

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

Mick White

neo said:
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
 
R

RobG

neo said:
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());
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 25
Dec 2004 00:28:01, seen in Jim Ley
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,
 
I

Ivo

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
 
L

Lee

Ivo said:
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.
 
I

Ivo

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
 
L

Lee

Ivo said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue,
28 Dec 2004 03:36:54, seen in Ivo
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.
 
J

John G Harris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top