spacer

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

home / experts / javascript / column2


Date Formats

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

There are many different date formats. Some consist only of numbers (e.g., 31/12/97), while others include real month names (e.g., December 31, 1997). There are many distinct date formats. In this section we'll show you how to display the date in a Month Day, Year format. This convention is usually better than a "number-only" one, due to the fact that different countries have different ways of writing the date in a "number-only" format. For example, the last day of the millenium could be written in one of the following ways:

  • 31/12/99 (in Israel, for example)
  • 12/31/99 (in the US, for example)

What does 9/10/97 stand for? September 10, or October 9? It depends on where you live. The following script prints the current date in a Month Day, Year format.


<SCRIPT LANGUAGE="JavaScript">
<!--

function makeArray() {
  var args = makeArray.arguments;
  for (var i = 0; i < args.length; i++) {
    this[i] = args[i];
  }
  this.length = args.length;
}

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

function getString(date) {
  var months = new makeArray("January", "February",
"March", "April", "May", "June", "July", "August",
"September", "October", "November", "December");
  return months[date.getMonth()] + " " + date.getDate() +
", " + ((date.getYear() < 100) ? "19" : "") + date.getYear();
}

var cur = new Date();
fixDate(cur);
var str = getString(cur);
document.write(str);

// -->
</SCRIPT>


The makeArray() function is a user-defined constructor. It accepts a list of values (arguments) and puts them in an array. You could use the built-in Array() constructor instead, but it isn't supported by Navigator 2.0x.

The getString() function accepts a date, and returns a string in the Month Day, Year format. For example, it might return "May 5, 1980". The third global statment in the script assigns that string to a variable named str, and then prints it.

If you would like to display the date somewhere in an HTML table, you're in trouble. Never embed a script in a table, especially if it has a document.write() statement. At this time there is no workaround for this annoying bug (besides printing the entire table in JavaScript, or using Dynamic HTML).

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/format.html