By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,817 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,817 IT Pros & Developers. It's quick & easy.

Who's fault: different displays of date

P: n/a
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>

Sep 27 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Dung Ping wrote:
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>


Sep 27 '05 #2

P: n/a
Dung Ping wrote:
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.

<script type="text/javascript">
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:

<URL:http://www.merlyn.demon.co.uk/js-date0.htm#gY>

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

</script>

--
Rob
Sep 27 '05 #3

P: n/a
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.

Sep 28 '05 #4

P: n/a
Thank all for help.

Dung Ping

Sep 28 '05 #5

P: n/a
JRS: In article <UL*****************@news.optus.net.au>, dated Tue, 27
Sep 2005 23:40:36, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
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.

--
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.
Sep 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.