spacer

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

home / experts / javascript / column2


Instances of the Date Object

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

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