converting times in ColdFusion

I needed to change the time on some local web pages to reflect the time in Hawaii, but I am using HostmySite, which is based in Delaware, and since Hawaii is 6 hours behind of Delaware I needed to make a change.

to get this working is really simple, just by using some variables, one for the server time and another for the local time. Then just use dateadd function. since I know the server is 6 hours behind Hawaii, I just subtract 6 from the server time.

<cfset servertime = Now()>
<cfset time = Now()>
<cfset time = dateadd("h",-6,time) />

to display the server time you would use

#TimeFormat(servertime,'h:mm tt')#

to display the local time you would use

#TimeFormat(time,'h:mm tt')#

then I just display the results in a table

		<td>Server  Time</td>
		<td><cfoutput>#TimeFormat(servertime,'h:mm tt')#</cfoutput></td>
		<td>Hawaii Time</td>
		<td><cfoutput>#TimeFormat(time,'h:mm tt')#</cfoutput></td>

You can see this example working on my server

Leave a Reply

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