Converting To And From GMT / UTC Time In ColdFusion Using DateConvert()

Posted May 27, 2009 at 9:18 AM

Tags: ColdFusion

A few days ago, I started looking into using GMT (Greenwich Mean Time) and UTC (Universal Time Coordinated) times in ColdFusion. Specifically, I was looking into creating HTTP headers that required time to be sent in a special GMT-based HTTP time format. As I posted yesterday, I could easily get HTTP time stamps using GetHTTPTimeString(); this ColdFusion method would automatically convert from local time to GMT time and then format the date/time value for use with HTTP headers. I also demonstrated that you could use DateAdd() to convert to and from UTC time as well.

In the comments to my post yesterday, John Piotrowski pointed out that my DateAdd() calculations could be even more easily done with ColdFusion's DateConvert() method. From the documentation, DateConvert() takes a conversion type (Local2UTC / UTC2Local) and a date/time stamp. To make sure that this was an equivalent short-hand to DateAdd(), I ran a few quick tests:

 Launch code in new window » Download code as text file »

  • <!--- Get the current time. --->
  • <cfset dtNow = Now() />
  •  
  • <!---
  • Convert the date to GMT using DateAdd() and DateConvert()
  • to make sure that they are the same.
  • --->
  • DateAdd(): #DateAdd( "s", GetTimeZoneInfo().UTCTotalOffset, dtNow )#
  • <br />
  •  
  • DateConvert(): #DateConvert( "Local2UTC", dtNow )#
  • <br />

I this test, I am comparing DateAdd() and DateConvert() when converting the local time to UTC time. When I run the above code, I get the following output:

DateAdd(): {ts '2009-05-27 13:13:59'}
DateConvert(): {ts '2009-05-27 13:13:59'}

Exactly the same. Now, this is probably not needed at all, but I ran a test in the opposite direction as well just to make sure all was right with the world:

 Launch code in new window » Download code as text file »

  • <!--- Get the GMT time. --->
  • <cfset dtGMT = ParseDateTime(
  • GetHTTPTimeString( Now() )
  • ) />
  •  
  • <!--- Convert from GTM back to local time. --->
  • Now: #Now()#
  • <br />
  •  
  • DateConvert(): #DateConvert( "UTC2Local", dtGMT )#
  • <br />

When I run the above code I get the following output:

Now: {ts '2009-05-27 09:13:59'}
DateConvert(): {ts '2009-05-27 09:13:59'}

As you can see from the above demonstrations, DateConvert() is an effective shorthand for the combination of DateAdd() and GetTimeZoneInfo(). Thanks John Piotrowski, you rock!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page





Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »