Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | ColdFusion | Q3014 [ previous next ]

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

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


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.