Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q3014 How do I display the current date and/or time? Can I format it different ways?

You are here: irt.org | FAQ | ColdFusion | Q3014 [ previous next ]

Use the Now() function to obtain the current date and time from the server.

<cfoutput>#Now()#</cfoutput>

returns the date in the following format: {ts '1999-06-24 10:29:54'}

By using the DateFormat() function, we can tell ColdFusion that we want to see a date:

<cfoutput>#DateFormat(Now())#</cfoutput>

returns 24-Jun-99

Continuing onward, we can customize the date format even more by specifying a mask in the DateFormat() function.

<cfoutput>#DateFormat(Now(),"mm/dd/yy")#</cfoutput>

returns 06/24/99

<cfoutput>#DateFormat(Now(),"ddd, mmm d, yyyy")#</cfoutput>

returns Thu, Jun 24, 1999

<cfoutput>#DateFormat(Now(),"dddd, mmmm d, yyyy")#</cfoutput>

returns Thursday, June 24, 1999

The TimeFormat() function is similar to DateFormat(), except, of course, that it returns the time.

<cfoutput>#TimeFormat(Now())#</cfoutput>

returns 10:39 PM

<cfoutput>#TimeFormat(Now(),"hh:mm:ss tt")#</cfoutput>

returns 10:40:23 PM

<cfoutput>#TimeFormat(Now(),"HH:mm:ss tt")#</cfoutput>

returns 22:40:23. (Note: The capital letters "HH" tells CF to display using a 24-hour clock.)

<cfoutput>
#TimeFormat(Now(),"h")# hours<br>
#TimeFormat(Now(),"m")# minutes<br>
#TimeFormat(Now(),"s")# seconds
</cfoutput>

This returns:
10 hours
43 minutes
43 seconds

©2018 Martin Webb