The School for Champions is an educational website that shows you how to achieve your dreams.
![]() |
|
|
SfC Home > ColdFusion > Explanation of how to deal with dates and times when using the ColdFusion markup language. Also refer to database, SQL, filtering, query, format, ODBC, forms, submit, Ron Kurtus, School for Champions. Copyright © Restrictions Dealing with Dates and Times in ColdFusionby Ron Kurtus (revised 17 December 2001) Dates and times are used in many ColdFusion applications. Depending on how your database is set up, you may have to use special formatting to be see the dates correctly, to use them as a filter in a query, or when submitted through a form. Questions you may have include:
This lesson will answer those questions. There is a mini-quiz near the end of the lesson. Displaying output of queryThe output of your query depends on the type of data fields you use in your database. If you are using a date/time field, you may have to format your output. Dates and times in databaseYou can enter dates and/or times in a database in either a text field or a date/time field. Text fieldIf your database has dates and/or times located in a text field or column, there are no problems when you query the database for those items. The date is simply considered text. Date/time fieldUsually dates and times are entered in a date/time database column. This allows you to use other date/time functions, such as making comparisons, counting days, etc. Unfortunately, there are some database quirks concerning inputting and outputting data in this format that can cause problems. ColdFusion outputAlthough your database may store the date as a short date of date/time type, the output of a ColdFusion query will often result in the date being in the DateTime format of 2001-02-18 00:00:00. To display the date, you must reformat it. Check database output formatA good idea is to query for a specific date and time and then check the output to see what form it is in. <CFQUERY DATASOURCE="xxx" NAME="info"> Then check the output: <CFOUTPUT QUERY="info"> If the date is in the format similar to 2001-02-18 00:00:00, it is in the DateTime format, and you must format the date to view or use it. Likewise, if the time looks like 1899-12-30 19:00:00, it must be formatted. Display in proper formatTo display the date and time in the proper format, you can use: <CFOUTPUT QUERY="info"> The output should result in something like: 3/25/2001 Using date as filter in a queryIn order to use a date or time as a filter in a SQL query, it must be in an ODBC format. If you have seen its output is in the DateTime format, you should reformat your date using the CREATEODBCDATETIME and CREATEODBCTIME functions to put the them in a form that is readable through a SQL query. Format for ODBCFor example, you could set 3/25/2001 as a new_date variable and 7:30 PM as a new_time variable in this ODBC format. <CFSET new_date = #CREATEODBCDATETIME("3/25/2001")#> Filter queryThen you can filter your query: <CFQUERY DATASOURCE="xxx" NAME="activities"> Reformat in outputWhen you want to display the output, you must format the date and time for the proper appearance: <CFOUTPUT QUERY="activities"> The output would look something like: 3/25/2001 Submitting date and time through a formIf you want to submit a date and time through a form, it is good to validate that the user entered the date in a correct format. You use the <CFFORM> tag in order to do the validation. Validate inputThe following code illustrates how to validate date and time inputs: <CFFORM METHOD="POST" ACTION="actionpage.cfm"> Validate dateVALIDATE="date" results in validating that what what is typed in the text box is in the proper date format. If it is not, a warning message will pop up when the Submit button is pressed. Note that ColdFusion allows the year to be either 2001 or 01. REQUIRED="yes" will ensure the user inputs data in the text box. If no data is submitted, the warning message will display. Validate timeThe only problem with validating time is that ColdFusion uses a 24 hour clock, instead of AM and PM. It might be better not to validate the time and hope the user enters the data correctly. Sample formThe form would appear as below. Since this is an HTML page, instead of a CFML page, the functionality of validation is not available here. Another methodYou can also check that the date sent through the form is in the correct format in the action.cfm page, by including code similar to: <CFIF IsDate(form.date_used) IS "No"> This method seems a little awkward, but it may be useful in some situations. Inserting or updating dates and timesIf you are going to submit a form to insert a date or time in a database table column, or you are going to update a record with a date or time, you must reformat the data before making the insertion or update. The reason is that the <INPUT> operation sends the date and time data as text, which usually will not be in the correct format for the database. This means that you cannot use the <CFINSERT> or <CFUPDATE> tags to perform the task. Rather, it must be done through a query, using SQL commands. Submit action pageLet us submit a form to the action page, inputting a date and time. <CFFORM METHOD="POST" ACTION="actionpage.cfm"> Set format for SQLThen, before the data is inserted or updated, it must be set into the proper format for use in SQL. <CFSET new_date = #CREATEODBCDATETIME(date_used)#> Insert dataBecause you have created new functions, you cannot use the CFINSERT or CFUPDATE tags. They automatically use what was sent via the form to this page. Instead, you must use a query and then insert or update with SQL commands: <CFQUERY DATASOURCE="xxx"> Updating is done the same way. SummaryIt is a good idea to check to see how your data looks from a raw query. Then you can format the date and time data into the proper format. When you submit a date or time through a form, you can validate that they are being sent in the proper format in your form. To use a date or time to filer in a query or to insert or update the date or time in the database, you must set the item to be in the ODBC format to properly use it with the database. Seek to better society with your work ResourcesThe following resources provide information on this subject. WebsitesBooksMiscellaneousMini-quiz to check your understanding1. How do you output a date queried from date/time field in a database? 2. What is wrong with using validation when inputting time through a form? 3. Why can't you use CFUPDATE with dates or time? If you got all three correct, you are on your way to becoming a Champion in ColdFusion Development. If you had problems, you had better look over the material again. What do you think?Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. We will try to get back to you as soon as possible. Share linkFeel free to establish a link from your website to pages in this site. Or use our form to send this link to yourself or a friend. Students and researchersThe Web address of this page is Please include it as a reference in your report, document, or thesis. Where can you go from here?
|
The School for Champions helps you become the type of person that can be can be called a Champion.