Mastering JavaScript Dates: Date Formats - Doc JavaScript | WebReference

Mastering JavaScript Dates: Date Formats - Doc JavaScript


Date Formats

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:

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  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() 

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).

https://www.internet.com


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

URL: https://www.webreference.com/js/column2/format.html