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.

Date
First we are going to be working with getting the date onto your page.

Step 1
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.

Quote:
<cfset TheDate = Now()>

Step 2
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.

Quote:
<cfoutput>
#DateFormat(TheDate, ‘yyyy-mm-dd’)#
</cfoutput>

Time

Step 1
Just like the date we want to add the following code to detect what the current time is

Quote:
<cfset TheTime = Now()>

Step 2
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.

Quote:
<cfoutput>
#TimeFormat(TheTime, ‘HH:mm:ss’)#
</cfoutput>

Thats it for time and dates

Leave a Reply

Your email address will not be published. Required fields are marked *

About Host Media

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.

Category

ColdFusion/Railo & MySQL

Tags