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




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »
Nov 6, 2009 at 4:53 PM
How To Unformat Your Code (Like A Pro)
I tried to go *back* the other way. Adding formatting is actually a much more complicated problem than removing formatting. Anyway, here is what I could put together with a minimal amount of time: ... read »
Asaf
Nov 6, 2009 at 2:35 PM
ColdFusion GetPageContext() Massive Exploration
Hi, I actually found this post useful. I recently acquired a SSL certificate for my website and when I switched over to HTTPS Internet Explorer would throw an error when trying to download a dynamic ... read »
Nov 6, 2009 at 2:19 PM
How To Unformat Your Code (Like A Pro)
@Chuck, @Nathan, Well, now I feel like it's a challenge.... I accept. ... read »
Nathan Stanford
Nov 6, 2009 at 2:06 PM
How To Unformat Your Code (Like A Pro)
@Chuck, I would love it as well. I was not joking I am very serious I love well formatted code. ... read »
Chuck
Nov 6, 2009 at 1:54 PM
How To Unformat Your Code (Like A Pro)
Ben great job, I happen to like your coding style and find it very easy to understand and follow. @Nathan, I totally agree, I (like the rest of you I assume) am pretty meticulous when it comes to c ... read »