Text Size

ColdFusion

Setting the default date to GMT in a CFWheels ColdFusion App

I've been working on an app in ColdFusion for a client that needs to use dates/times across multiple countries, and thought I'd post this in case anyone else needs it. We are using CFWheels and I needed a way to ensure that the dates the framework automatically creates for the createdat and updatedat fields are in GMT time. There are a couple of  ways to do this, the first being to set your ColdFusion JVM settings to include a default timezone using an argument like "-Duser.timezone=GMT" in the JVM arguments fields of the CF Administrator. However, this will then set it for all apps on the ColdFusion server. If you only need it for the one CFWheels app, you can override the following method in your Model.cfc class:

1
2
3
4
5
6
7
<!--- Override the default timestamping with the GMT time --->
<cffunction access="public" name="$timestampProperty" output="false" returntype="void">
        <cfargument name="property" required="true" type="string">
                <cfscript>
                        this[arguments.property] = DateConvert(&quot;local2utc&quot;, now());
                </cfscript>
</cffunction>

This will ensure that all date values saved to the database will useUTC/GMT time.