Who's fault: different displays of date

D

Dung Ping

The following code produces different dates on different browers on my
computer.

For today on the IE it is: 2005.9.27 which is correct, but on FF is:
105.9.27 which in incorrect.

I copied the code free from somewhere. Are the different displays the
fault of browser, computer or code? Thanks.

<script type="text/javascript" language="javascript">
var today_date= new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth()+1
var mytoday=today_date.getDate()

document.write("Today is: ")
document.write(myyear+"."+mymonth+"."+mytoday)
</script>
 
D

Dung Ping

By the way, the page containing the code is in:

www.pinyinology.com

The date in discussion is at the right lower corner of the page, with
black background and white writing. I changed the Chinese language to
'Today is: ' in English wben posting it to this forum. But the results
are same.
 
L

Lee

Dung Ping said:
The following code produces different dates on different browers on my
computer.

For today on the IE it is: 2005.9.27 which is correct, but on FF is:
105.9.27 which in incorrect.

I copied the code free from somewhere. Are the different displays the
fault of browser, computer or code? Thanks.

<script type="text/javascript" language="javascript">
var today_date= new Date()
var myyear=today_date.getYear()

It's the fault of the code.
Replace "getYear()" with "getFullYear()".

The getYear() method was originally specified to return the current
year with 1900 subtracted from it (just like the corresponding
function in the standard C library, among others).

As the year 2000 approached, some browser developers, knowing that
few Javascript coders read the documentation, began making ill-
conceived changes to the method in order to try to make it do what
most people expected.

As a result, getYear() does different things in different browsers,
and even in different versions of the same browser.
 
R

RobG

Dung said:
The following code produces different dates on different browers on my
computer.

For today on the IE it is: 2005.9.27 which is correct, but on FF is:
105.9.27 which in incorrect.

It is correct, it's just not what you expect. Read the ECMA spec
section 15.9 Date Objects.
I copied the code free from somewhere. Are the different displays the
fault of browser, computer or code? Thanks.

<script type="text/javascript" language="javascript">

The language attribute is depreciated, type is required.

var today_date= new Date()
var myyear=today_date.getYear()

It is also a good idea to always use semi-colons to end statements, even
though they aren't always required in the source code.

getYear() returns the current year - 1900, so the result you get is
exactly what the spec says you should get. If you want 2005 rather than
105 (and who wouldn't), use getFullYear():

var myyear=today_date.getFullYear();

getFullYear is not supported by very old browsers, so if you want to
accommodate them, use one of the routines suggested here:

var mymonth=today_date.getMonth()+1
var mytoday=today_date.getDate()

document.write("Today is: ")
document.write(myyear+"."+mymonth+"."+mytoday)

This is more efficient (though undetectably so) if written using a
single call to document.write:

document.write(
"Today is:" + myyear + "." + mymonth + "." + mytoday);
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 27
Sep 2005 23:40:36, seen in RobG
Dung Ping wrote:

To answer his Subject line - the fault lies with the stupidity of
primeval programmers, probably in California.

The value of Full Year will have been available from operating systems,
so getYear should have been coded as getFullYear in the first place, in
order not to lose information which some will need; then to get a two-
digit year is trivial for the script coder, using %100.

getFullYear is not supported by very old browsers, so if you want to
accommodate them, use one of the routines suggested here:
<URL:http://www.merlyn.demon.co.uk/js-date0.htm#gY>

That may be the right place to start, but the routines are in #gFY.

That does not include, though it does link to, Year = 2000 + Year%100
which should do for our lifetimes.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top