spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / pharmacy / article1


Overcoming Different Year Conventions

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

For dates prior to 2000, the getYear() method returns the year minus 1900, so the easiest way to display the four-digit year is to use the getFullYear() method:

var now = new Date();
var num = now.getFullYear();
alert(year);

The following script segment computes the year for a different date than the current:

var xmas1 = new Date("December 25, 1998 00:00:01");
var xmas2 = new Date("December 25, 2000 00:00:01");
var year1 = xmas1.getFullYear();
var year2 = xmas2.getFullYear();
alert(year1 + ", " + year2);

Click the following button to see the script in action:

It's possible to retrieve the current two-digit date:

var now = new Date();
var str = now.getFullYear() + "";
var year = eval(str.substring(str.length - 2, str.length));
alert(year);

This script converts the current year into a string by attaching an empty string to the value returned by the getFullYear() method. It then extracts the last two characters of the string via the substring() method. The eval() function converts the new substring into a two-digit number. The following script segment displays the current century (e.g., 20 if the year is 1998, 21 if the year is 2098):

var now = new Date();
var str = now.getFullYear() + "";
var century = (str.length == 2) ? 20 : eval(str.substring(0, 2)) + 1;
alert(century);

If the string assigned to str consists of two characters, the current year is in the 20th century (e.g., 1998). Otherwise, the script converts the two first characters of the string into a number (using the eval() function). The following demonstration utilizes this technique:

var xmas1 = new Date("December 25, 1998 00:00:01");
var xmas2 = new Date("December 25, 2098 00:00:01");
var xmas3 = new Date("December 25, 2198 00:00:01");
var str1 = xmas1.getFullYear() + "";
var str2 = xmas2.getFullYear() + "";
var str3 = xmas3.getFullYear() + "";
var century1 = (str1.length == 2) ? 20 : eval(str1.substring(0, 2)) + 1;
var century2 = (str2.length == 2) ? 20 : eval(str2.substring(0, 2)) + 1;
var century3 = (str3.length == 2) ? 20 : eval(str3.substring(0, 2)) + 1;
alert(century1 + ", " + century2 + ", " + century3);

Click the following button to view the script's output:

Now take a look at a statement from Microsoft's JScript site:

"The year is an integer value and is returned as the difference between the stored year and 1900. For example, 1996 is returned as 96, and 2025 is returned as 125."

Internet Explorer, Microsoft's browser, does not behave this way. It seems to agree with Netscape's documentation. That is, it returns a four-digit year for years greater than 1999. For years before 1900, which are supported in Internet Explorer (as opposed to Navigator), the getYear() method returns a full four-digit year. Netscape Navigator itself does not match Netscape's documentation and returns the difference from 1900 for years greater than 1999.


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2 · Create Multilingual Web Sites with Windows Unicode Fonts
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MySql View Technique for Grouping Crosstab Column Data · Why You Need a Mobile Web Site · Tame Web Marketing with Social Media Management

Created: February 4, 1997
Revised: June 21, 2000

URL: http://www.webreference.com/js/pharmacy/article1/overcome.html