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

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

*
You can see this example working on my server

Leave a Reply