The Date Widget

Templating System : Widget Reference : Date

Overview

The date widget provides a versatile HTML control for entering dates in a variety of formats. The widget operates in conjunction with various template::util::date functions in order to validate and manipulate the user's input. Please see the demo pages for some examples of the widget's behavior.

The Date Object

The widget's value is a Date object, defined in template::util::date. The date object stores 7 fields: the year, month, day, hours, minutes, seconds, and the format in which these values should be displayed. The function template::util::date::create can be used to instantiate a blank date:

proc template::util::date::create {
  {year {}} {month {}} {day {}} {hours {}} 
  {minutes {}} {seconds {}} {format "YYYY/MM/DD"}
} {
  return [list $year $month $day $hours $minutes $seconds $format]
}

The two functions template::util::date::get_property and template::util::date::set_property are used to get or set the fields of a Date object. The get_property function accepts the desired field and the Date object, and returns the value of the field:

proc template::util::date::get_property { what date } {
...
}

The set_property function accepts the field, the Date object and the new value, and returns the modified Date object:

proc template::util::date::set_property { what date value } {
...
}

The fields which can be accessed or changed are summarized below:

FieldGet ?Set ? MeaningSample Value
yearYesYes The 4-digit year2000
monthYesYes The month, January = 18
dayYesYes The day of month21
hoursYesYes The hour, in 24-hour format 23
minutesYesYes The minute59
secondsYesYes The second59
formatYesYes The format (see below for a detailed explanation)YYYY/MM/DD
long_month_nameYes  The symbolic month nameJanuary
short_month_nameYes  The abbreviated month nameJan
days_in_monthYes  The number of days in the month stored in the Date object; will return an empty string if the month or the year are undefiend. Takes into account the leap years. 29
short_yearYesYes The 2-digit year. When mutating, 2000 is added to the year if it is less than 69; otherwise, 1900 is added to the year. 99
short_hoursYesYes The hour, in 12-hour format. When mutating, the hour is always assumed to be in the "a.m." range; the ampm field may be used to change this. 3
ampmYesYes The meridian indicator: either am or pm. Can be used in conjunction with the short_hour field in order to completely specify the hour. am
not_nullYes  This field will be 1 if and only if at least one of the date fields (year, month, date, hours, minutes or seconds) is present in the Date object. Otherwise, this field will be 0. 1
sql_dateYes  The SQL code fragment representing the date stored in the Date object. to_date('2000/08/12 11:15:00', 'YYYY/MM/DD HH24:MI:SS')
clockYesYes The value of the date in the same format as the value returned by the clock seconds function. The clock function appears to be locale-dependent and therefore unreliable; however, manipulating the clock value with clock scan is currently the only way to perform arithmetic operations on dates, such as adding a day, comparing two dates, etc. (An integer representing the number of elapsed seconds)

For example, the following code produces the tomorrow's date in SQL:


# Create a blank date
set today_date [template::util::date::create]

# Get the tomorrow's date
set clock_value [clock scan "1 day" -base [clock seconds]]
set tomorrow_date [template::util::date::set_property \
  clock $today_date $clock_value]

# Get the SQL value
set tomorrow_sql [template::util::date::get_property \
  sql_date $tomorrow_date]

The Date Element

The widget is created with the usual template::element create statement, with the datatype and widget set to date. In addition, the element requires a -format switch, which specifies the format for the date, as follows:

OptionFormatMeaning
-format long YYYY/MM/DD HH24:MI:SS The full widget including the date and the time
-format short YYYY/MM/DD The widget capable of entering the date only, without the time
-format time HH24/MI/SS The widget capable of entering the time only, without the date
-format american MM/DD/YY The widget representing the more familiar American date
-format expiration DD/YY An expiration date, as it may appear on a credit card
-format custom string Custom format See below

Any other value for the format flag is interpreted as a custom format string. The custom format string should consist of format specifiers separated by any of /\-.: or spaces. The valid format specifiers are as follows:
Format SpecifierFieldDefault Widget
YYYYyearInput box, 4 characters
YYshort_yearInput box, 2 characters
MMmonthSelection list
MONmonth Selection list of abbreviated month names
MONTHmonth Selection list of full month names
DDdaySelection list
HH12short_hours Selection list from 1 to 12
HH24hours Selection list from 0 to 23
MIminutes Selection list from 0 to 60, skipping every 5 minutes
SSseconds Selection list from 0 to 60, skipping every 5 seconds
AMampm Selection list of "A.M." and "P.M."

Any format specifier may be followed by a lowercase t, in order to force the widget to use an input box (instead of a selection list) for entering the specified date fragment.

The -format switch is required, but the date widget also supports the following optional switches:

SwitchMeaningExample
-field_interval interval Specifies a custom interval for the given field, as a list of three values: the starting value, the ending value, and the step -minute_interval {0 59 5}
-help Causes the date widget to display a description of each date fragment widget showing the purpose of the widget, such as "Year" or "24-Hour" -help

Examples of various Date widgets can be found on the demo pages.


templating@arsdigita.com