CF_convertDatev1.0September 2003© Claude Schnéegans |
|---|
| DESCRIPTION |
|---|
Struggling with dates in ColdFusion can be sometimes quite an hazardeous adventure. The problem, or should we say the many problems, have several origins:
It also comes in two flavors: a standard custom tag compatible with all ColdFusion versions, and a function for CF 5+. Both are included in the same package.
| NOTE |
|---|
| INSTALLATION |
|---|
The other files are this documentation (convertDateDoc.cfm) and a small program to test the tag: testConvertDate.cfm; just store them in any convenient place in the HTTP area on your development server.
If you prefer to use the function version, just include the file convertDateFN.cfm in your template, or cut and paste its content into your own program.
| CUSTOM TAG VERSION SYNTAX |
|---|
<CF_convertDate inputVariable = "" inputType = "date|string" outputType = "date|stringODBCdate" inputMask = "" outputMask = "" outputVariable = "" > |
| TAG | ATTRIBUTE | CONTENT | REQUIRED | DEFAULT |
|---|---|---|---|---|
| <CF_convertDate | inputVariable= | string containing name of the variable to validate and convert; since the tag can accept not only strings representing a date, but a true date type variable, and since only a string ca be passed in an attribute, this attribute contains the name of a variable containing a date. | Yes | |
| =inputType | Type of the date transmited in the input variable. It may be either "string" or "date". | No | "string" | |
| =outputType | Type the date should converted to in the output variable. It may be either "string", "date" or "odbcdate". | No | "odbcdate" | |
| inputMask= | Mask specifying the format of the date as to be found in the input variable if it is a string; ex: "dd/mm/yyyy" or mm/dd/yyyy. If the part for the year has only two digits, the year will be assumed using the same rule as for the function ParseDateTime (), ie: Year values 0 - 29 are interpreted as 21st century dates. Year values 30 - 99 are interpreted as 20th century dates. "US" and "EU" are accepted as short cuts respectively for "mm/dd/yyyy" and "dd/mm/yyyy". If the variable contains a date of type "date", this attribute is not used. | No | "mm/dd/yyyy" | |
| outputMask= | Mask specifying the format the date should be converted to. This attribute is used only if outputType="string". | No | "mm/dd/yyyy" | |
| outputVariable= | Name of a variable in the calling template to receive the result. | No | "outputDate" |
| ERROR HANDLING |
|---|
The tag also validates the date submitted in input. If no error is detected, the caller's variable specified in the attribute variable contains the output date. Another variable is also defined in the callers's scope by the tag: dateError will contain an error message, or an empty string if there is no error. One can then check the content of this variable to decide if the program may proceed with the converted date or if some error message should be returned to the user.
| FUNCTION VERSION SYNTAX |
|---|
convertedDtate = convertDate ( inputDate, inputType, outputType, [inputMask], [outputMask]) |
| inputDate | Date to be converted. May be a string like "09/01/2003" or a date like now() |
| inputType | Type of the date to be converted; may be "string" or "date". |
| outputType | Type of the returned converted date. Should be "string", "date" or "odbcdate" |
| inputMask | Mask specifying the format of the date as to be found in the input variable if it is a string; ex: "dd/mm/yyyy" or mm/dd/yyyy. If the part for the year has only two digits, the year will be assumed using the same rule as for the function ParseDateTime (), ie: Year values 0 - 29 are interpreted as 21st century dates. Year values 30 - 99 are interpreted as 20th century dates. "US" and "EU" are accepted as short cuts respectively for "mm/dd/yyyy" and "dd/mm/yyyy". If the variable contains a date of type "date", this parameter is not used. |
| outputMask | Mask specifying the format the date should be converted to. This attribute is used only if outputType="string". |
| ERROR HANDLING BY THE FUNCTION |
|---|
Normaly, the function will return the converted date in a simple value. If an error was encountered, the function returns a structure instead. The structure contains only one element: .message.
After the call to the function, the programer should then test if the returned value is a structure (see examples).
| EXAMPLES |
|---|
Validates a date entered in American format and displays it in full format.
Note that dates like "15/10/2003" which are valid in European format and accepted by the isDate() function, will be refused by convertDate.
Custom tag version:
<CF_convertDate inputVariable="form.myDate" inputMask="US" outputMask="ddd, mmmm dd, yyyy"> <CFOUTPUT> <CFIF dateError EQ ""> The date is #outputDate# <CFELSE> Ooops! Your date is invalid: #dateError#. </CFIF> </CFOUTPUT>Function version:
<CFSET outputDate = convertDate(form.myDate, "string", "string", "EU", "ddd, mmmm dd, yyyy")> <CFOUTPUT> <CFIF NOT isStruct(outputDate)> #outputDate# <CFELSE> >Ooops! Your date is invalid: #outputDate.message#. </CFIF> </CFOUTPUT>
Validates and converts a date received in a form in European format ("dd/mm/yyyy") to ODBC format in order to insert it in a table:
Custom tag version:
<CF_convertDate inputVariable="form.myDate" inputMask="EU">
<CFIF dateError EQ "">
<CFQUERY DATASOURCE="myDatasource">
INSERT INTO myTable
VALUES (#outputDate#)
</CFQUERY>
<CFELSE>
<CFOUTPUT>Ooops! Your date is invalid: #dateError#.</CFOUTPUT>
</CFIF>
Function version:
<CFSET outputDate = convertDate(form.myDate, "string", "odbcdate", "EU")>
<CFIF NOT isStruct(outputDate)>
<CFQUERY DATASOURCE="myDatasource">
INSERT INTO myTable
VALUES (#outputDate#)
</CFQUERY>
<CFELSE>
<CFOUTPUT>Ooops! Your date is invalid: #outputDate.message#.</CFOUTPUT>
</CFIF>
If the date has been already validated in the client browser, the test for errors can be bypassed, and the function can be used directly in a query. This can be a particularily simple and handy way to manage dates entered in European (aka ISO) format and make sure they are properly converted in ODBC format for the query.
<CFQUERY DATASOURCE="myDatasource"> INSERT INTO myTable VALUES (#convertDate (form.myDate, "string", "odbcdate", "EU")#) </CFQUERY>
Here is a small test program that reads values for the attributes and apply them to the tag and the function as well. If you select an input type of "date" and leave the "Enter a date" field blank, a variable initialized with Now() will be used.