WillMaster Possibillites Logo EzineSeek Award
JavaScript Clocks and Calendars
Part II
Custom Date/Time Display

by
Mari Bontrager

Permission is granted to reprint this article in its entirety, provided no reprints are sent in conjunction with unsolicited bulk email, provided no fee or other value is exchanged, provided no changes are made to the article, and provided the author's name, signature lines, and copyright line are printed with the article; except you may change the article's title.

This article shows you how dates can be printed in any language, with or without day of the week, using JavaScript. Date and time can be in any format you prefer.

Part I of this series, now published at http://willmaster.com/a/15/pl.pl?jsccart1 shows how to display the current time, with any custom format, as the text of a form button. This Part II shows how to display the date and day of the week, in addition to the time, in a form text field. (The form button code can now modified to include date information.)

If you want to bypass the tutorial aspects of this article, you can download the source code immediately from the demonstration page at http://willmaster.com/a/15/pl.pl?jscc2

The Page Display

When you view the source code of the demonstration page, you'll notice that the form field input tag contains a CSS style attribute specifying the text to be centered in the form field. The centering will work with IE5+ and Netscape 6+ browsers.

The apparent size of the input text field will vary from browser to browser, even when you specify a size. Usually, the date/time display will be shorter than the available space in the text field. If you do not want the text to center in the field, simply remove the style attribute.

The size of the text field should be specified as the number of characters of the longest possible display. In the English language, Wednesday is the day of the week with the largest number of characters and September is the month. Therefore, to calculate the size of the text field for the demo page, I counted the number of characters in:

Today is Wednesday, September ##, #### at ##:##:## AM

The above is 53 characters long. Therefore, I put size="53" into the form field input tag.

The Multi-Lingual Aspect

The JavaScript source code lets you specify the names of the months and the days of the week. These can be in any written language. They can be spelled out or abbreviated.

The Display Formatting Function

The time display formatting function developed for Part I of this series has been expanded to also format dates, with or without the day of the week. It works the same way; what you specify as the DateTimeFormat will print on the page. Example:

DateTimeFormat = "WEEKDAY, MONTH DAY, YEAR at H24:0M:0S";

Before printing, however, any placeholders found in the DateTimeFormat will be replaced with live data (all other characters in the template are left alone). Here are the placeholders:

Placeholder   Is replaced with
===========   ================
     YEAR     The 4-digit year.
       YR     The 2-digit year.
    MONTH     The month name.
       MT     The month number.
      0MT     The month number, with a zero leading single 
                 digits.
  WEEKDAY     The day of the week.
      DAY     The day of the month.
     0DAY     The day of the month, with a zero leading 
                 single digits.
      H12     The hour (12-hour clock).
     0H12     The hour, 12-hour clock, with a zero leading 
                 single digits.
      H24     The hour (24-hour clock).
     0H24     The hour, 24-hour clock, with a zero leading 
                 single digits.
       MM     The minute.
       0M     The minute, with a zero leading single digits.
       SS     The second.
       0S     The second, with a zero leading single digits.
     AMPM     "AM" if before noon, "PM" otherwise.

The "0" characters in the placeholders are zeros, not Ohs. Also, placeholders are case sensitive.

Assuming June 8, 2005 at 3:06:09 PM, the following formats will yield:

Today is WEEKDAY, MONTH DAY, YEAR at H12:0M:0S AMPM
Today is Wednesday, June 8, 2005 at 3:06:09 PM

0MT/0DAY/YR
06/08/05

0DAY.0MT.YR at H24:0M:0S
08.06.05 at 15:06:09

You can see how it works on the demonstration page at http://willmaster.com/a/15/pl.pl?jscc2

With the above, you'll be able to display dates and times in near inexhaustable variations.

By: Will Bontrager

Copyright 2002 Bontrager Connection, LLC
http://willmaster.com/possibilities/
subscribe-possibilities@willmaster.com