Skip to main content
Ben Nadel at the ColdFusion Centaur And Bolt User Group Tour (Jun. 2009) with: Ben Forta
Ben Nadel at the ColdFusion Centaur And Bolt User Group Tour (Jun. 2009) with: Ben Forta

Parsing Http Time Strings With ParseDateTime() In ColdFusion

By
Published in Comments (2)

This is a super minor post; but, last week, when I was working on Sticky CDN, I remembered that the parseDateTime() function, in ColdFusion, could parse the date/time strings commonly used in HTTP headers. parseDateTime() won't take the timezone information into account; but, it will parse the date and time portions properly.

To see this in action, I've got a demo that takes my local development time (EST), generates the Http Time String, then parses it (as UTC), and converts it back to local time:

<cfscript>

	/**
	* I output the date with both date and time portions formatted.
	*
	* @output false
	*/
	public string function outputTime( required date input ) {

		return(
			dateFormat( input, "Mmm d, yyyy" ) &
			" at " &
			timeFormat( input, "h:mm:ss TT" )
		);

	}


	// ------------------------------------------------------ //
	// ------------------------------------------------------ //


	// Get the current time on my local development machine (EST).
	easternTime = now();

	// Get the given time, converted to UTC, in a format according to (documentation):
	// ---
	// "HTTP standard described in RFC 1123 and its underlying RFC, 822. This format is
	// commonly used in Internet protocols, including HTTP."
	httpTime = getHttpTimeString( easternTime );

	// Now, try to parse it back into a date (UTC).
	parsedTime = parseDateTime( httpTime );

	// And, convert the UTC date back to my local development time.
	localTime = dateConvert( "utc2local", parsedTime );

	// Output results.
	writeOutput( "Http Time: #httpTime#<br />" );
	writeOutput( "- - - -<br />" );
	writeOutput( "Original Time: #outputTime( easternTime )#<br />" );
	writeOutput( "Parsed Time: #outputTime( parsedTime )#<br />" );
	writeOutput( "Local Time: #outputTime( localTime )#<br />" );

</cfscript>

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

Http Time: Mon, 04 Aug 2014 11:00:35 GMT
- - - -
Original Time: Aug 4, 2014 at 7:00:35 AM
Parsed Time: Aug 4, 2014 at 11:00:35 AM
Local Time: Aug 4, 2014 at 7:00:35 AM

Again, the parseDateTime() function won't take the timezone into account; so, if I were to replace "GMT" with "EST" before parsing it, nothing would change. But, since the standard seems to be that HTTP headers are all in UTC/GMT time, then you simply need to convert the parsed time to your local time (if so desired).

Want to use code from this post? Check out the license.

Reader Comments

41 Comments

This code will work in most versions of ColdFusion. You could shorten the code a bit by using the DateTimeFormat() function in CF10 and CF11.

15,848 Comments

@Brian,

It's funny you mention that. I actually started off trying to use the dateTimeFormat(). But, I was getting a really odd output. I had a mask that was something link:

"Mmm d, yyyy 'at' h:nn:ss TT"

... but the 'at' kept getting converted to 'p'. Not sure why. Even the examples in the docs use the '' notation to escape portions of the mask. It had me flummoxed - I couldn't figure it out, so just reverted to something more manual.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel