I am going to be going into how to get the current date & time with coldfusion, dont worry it is very simple to do and does not require anything special like a datasource.
First we are going to be working with getting the date onto your page.
First within your Application.cfm/.cfc we want to add the following cfset to get the date. If you wish you can add this to the same page as the output of the date but it is always nice to have an application file.
|<cfset TheDate = Now()>|
The following code mainly formats how the date gets displayed, the sample code below will output the date in the format of YYYY/MM/DD but you can easily turn this into: DD/MM/YYYY.
Just like the date we want to add the following code to detect what the current time is
|<cfset TheTime = Now()>|
Again same as the date we want to format the time in to something we can display and read on screen. There is something that is very different to the date and that is using capitol letters, when you have for example ‘HH’ instead of ‘hh’ for the hour to are saying to the coldfusion to use a 24 hour system with the hours. Below we have set the time to display in a 24 hour clock with HH:MM:SS you can take out the ‘SS’ if you do not wish to display the seconds.
Thats it for time and dates
We have been hosting websites since 2002 and are always moving forward. All articles written under the Host Media author are created by the team who support our customers.