Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Standard

Status
Not open for further replies.

liyma

Programmer
Feb 25, 2002
1
0
0
US
Hi, All,

I'm new to Javascript, and was wondering whether there is a standard/specification for Javascript.
I wrote a simple statement "d.getYear()" in which d is current date. When running in IE, it returns 2002, but when running in Netscape, it returns 102!
I guess an unambiguous spec could help me determine which is right, and help me answer more questions to come.
Thanks in advance for your information.


 
Hi Liyma,

This is a good javascript reference for using Netscape:

On this page click on the "index" and after that scroll to "getyear".

As you can see getyear returns the year minus 1900 so that's why you get 102 !!
They say you have to use Date.getFullYear

Hope this site helps you in the future (LOL),
Erik

I copied the text for you :

getYear
Returns the year in the specified date according to local time.Method of
Date

Implemented in
JavaScript 1.0, NES 2.0


JavaScript 1.3: deprecated; also,

getYear returns the year minus 1900 regardless of the year specified

ECMA version
ECMA-262



Syntax
getYear()
Parameters
None

Description
getYear is no longer used and has been replaced by the getFullYear method.

The getYear method returns the year minus 1900; thus:


For years above 2000, the value returned by getYear is 100 or greater. For example, if the year is 2026, getYear returns 126.

For years between and including 1900 and 1999, the value returned by getYear is between 0 and 99. For example, if the year is 1976, getYear returns 76.

For years less than 1900 or greater than 1999, the value returned by getYear is less than 0. For example, if the year is 1800, getYear returns -100.
To take into account years before and after 2000, you should use Date.getFullYear instead of getYear so that the year is specified in full.

Backward Compatibility
JavaScript 1.2 and earlier versions. The getYear method returns either a 2-digit or 4-digit year:


For years between and including 1900 and 1999, the value returned by getYear is the year minus 1900. For example, if the year is 1976, the value returned is 76.

For years less than 1900 or greater than 1999, the value returned by getYear is the four-digit year. For example, if the year is 1856, the value returned is 1856. If the year is 2026, the value returned is 2026.
Examples
Example 1. The second statement assigns the value 95 to the variable year.

Xmas = new Date("December 25, 1995 23:15:00")year = Xmas.getYear() // returns 95
Example 2. The second statement assigns the value 100 to the variable year.

Xmas = new Date("December 25, 2000 23:15:00")year = Xmas.getYear() // returns 100
Example 3. The second statement assigns the value -100 to the variable year.

Xmas = new Date("December 25, 1800 23:15:00")year = Xmas.getYear() // returns -100
Example 4. The second statement assigns the value 95 to the variable year, representing the year 1995.

Xmas.setYear(95)year = Xmas.getYear() // returns 95
<!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
The getYear() method of the Date-object should return the years since 1900. To become the year, you have to add 1900 to it.
Most browsers, like NS4, NS6, Opera, ... do this very consistently.

In MSIE this is ONLY true up to 1999, so you'll have to test if it's a date after 1999, in wich case you don't have to add the 1900... (stupid from them, no?)

Try yourself:

--------------------------------------------

<script type=&quot;text/javascript&quot;>

today = new Date();
thisyear = today.getYear();
alert(thisyear); // returns 2002 in MSIE, but 102 in NS4/NS6/Opera

christmas = new Date(&quot;December 25, 1969&quot;);
thatyear = christmas.getYear();
alert(thatyear); // returns 69 both in MSIE and NS4/NS6/Opera

</script>

--------------------------------------------

To learn about the &quot;real&quot; javascript, take a look at:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top