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





Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »