Date and Time Basics: dateFormat() and timeFormat()
Formatting the data and time has a lot to do with the data we are presenting and the room available on the page. For some cases, we would want a fully written out date in the long format. Other times we want the day and month as short and sweet as possible. Still other times the month, day and year should be shown. We can use preceding zeros on single digit days and months to keep a consistent width in our layout, and choose 2 or 4 digit years. The time can also be displayed in nearly every way you can think of. ColdFusion an do all of this and much more with it's set of date and time functions that make this all very easy.
Formatting the Date
A unformatted date / time object looks something like this: {ts '2005-12-21 20:28:52'}. While that has a lot of great information, it is not very useful for the end user or general viewing.
ColdFusion function dateFormat() will take care of making the date look nice. DateFormat() takes to arguments: date, and mask. Date is a date time object that you would like to format. The function now() produces the current date and time as shown above in this paragraph. The mask argument is optional. If it is left out, the default format of dd-mmm-yyyy. Note that the date / time object contains the date AND time. The dateFormat() function ignores the time portion of the date / time. The function timeFormat() is talked about in this tutorial later.
According the ColdFusion 7 Reference book available from Macromedia, the following is used to mask the date format:
· d: Day of the month as digits; no leading zero for single-digit days.
· dd: Day of the month as digits; leading zero for single-digit days.
· ddd: Day of the week as a three-letter abbreviation.
· dddd: Day of the week as its full name.
· m: Month as digits; no leading zero for single-digit months.
· mm: Month as digits; leading zero for single-digit months.
· mmm: Month as a three-letter abbreviation.
· mmmm: Month as its full name.
· yy: Year as last two digits; leading zero for years less than 10.
· yyyy: Year represented by four digits.
· gg: Period/era string. Ignored. Reserved.
The following masks tell how to format the full date and cannot be combined with
other masks:
· short: equivalent to m/d/y
· medium: equivalent to mmm d, yyyy
· long: equivalent to mmmm d, yyyy
· full: equivalent to dddd, mmmm d, yyyy
To format the date in a common short format use the following
<cfoutput>
#dateFormat(now(),"short")#
</cfoutput>
If you want preceding zeros for days and months represented by only one digit, you will need to manually create a mask as shown.
<cfoutput>
#dateFormat(now(),"mm/dd/yy")#
</cfoutput>
You can use different characters to separate the parts of the date too.
<cfoutput>
#dateFormat(now(),"mm-dd-yy")#
</cfoutput>
Try these to see how the different masking characters display the date parts.
<cfoutput>
Short: #dateFormat(now(),"m/d/yy")#<br/>
Medium: #dateFormat(now(),"mm/dd/yyy")#<br/><br/>
Short Text: #dateFormat(now(),"mmm d, yy")#<br/>
Long Text: #dateFormat(now(),"dddd mmmm d, yyyy")#<br/>
</cfoutput>
Formatting Time
Formatting the time is basically the same as formatting the date. the function timeFormat() takes two arguments, the time, which can be taken from the now() function also, and the mask. The mask characters for timeFormat() as mentioned in the ColdFusion 7 Reference are:
· h: hours; no leading zero for single-digit hours (12-hour clock)
· hh: hours; leading zero for single-digit hours (12-hour clock)
· H: hours; no leading zero for single-digit hours (24-hour clock)
· HH: hours; leading zero for single-digit hours (24-hour clock)
· m: minutes; no leading zero for single-digit minutes
· mm: minutes; a leading zero for single-digit minutes
· s: seconds; no leading zero for single-digit seconds
· ss: seconds; leading zero for single-digit seconds
· l or L: milliseconds. l gives 3 digits. L gives 2 digits.
· t: one-character time marker string, such as A or P
· tt: multiple-character time marker string, such as AM or PM
· short: equivalent to h:mm tt
· medium: equivalent to h:mm:ss tt
· long: medium followed by three-letter time zone; as in, 2:34:55 PM EST
· full: same as long
The timeFormat() function works the same way as the dateFormat function.
<cfoutput>
#timeFormat(now(),"h:mm ss tt")#
</cfoutput>
____________