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  |  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
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »
Mar 19, 2010 at 4:15 PM
SQL COUNT( NULLIF( .. ) ) Is Totally Awesome
Thank you Ben and Tony! Either of these work for the summary report I am working on and the info is much appreciated! I think I like Tony's a little better because I won't have to educate every ... read »