spacer

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

home / experts / javascript / column2


Instances of the Date Object

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

JavaScript stores all dates as the number of milliseconds since January 1, 1970 00:00:00. Therefore, trying to handle preceding dates results in an error, or even crashes the browser.

JavaScript does not have a date data type. However, the Date object and its methods enable you to work with dates in your scripts. In order to take advantage of the Date object's features, you must first create an instance reflecting a specific date:

var varName = new Date([arguments]);

varName is a variable name or a property of an existing object. arguments can be one of the following:

  • Nothing (reflects the current date). For example, var current = new Date();.
  • A set of integer values for year, month and day. For example, var lastDay = new Date(99, 12, 31);.

There are several other possible arguments, but these are the most important ones. The Macintosh version of Netscape Navigator 2.0x is one day in the future. In order to fix this bug in your script, use the following function:

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

Since some of the methods in this function are beyond the scope of our column, we'll just show you how to use it. Once you've created an instance of the Date object, simply hand it to the function in the following fashion:

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

var current = new Date(); // a new instance
fixDate(current);

http://www.internet.com


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 >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger


Created: September 11, 1997
Revised: April 7, 1998

URL: http://www.webreference.com/js/column2/instance.html