Search code examples
datetimecoldfusioncoldfusion-2016

ColdFusion : Create Date Time with UTC


I am trying to use a UTC time, so that the user can get the time of things at their UTC, rather than the server.

I am currently setting the time with this : which works.

<cfset stime = (DateFormat( sdate, "yyyy-mm-dd" ) & " " & "#hr#:#mn# #HH#") />

I need to add the UTC Stamp of say -8.0 (PST).

(timeFormat(stime, "hh:mm:ss, Z"));

With Z being a default setting of UTC which I have.

How do I create the combined Date and Time to insert into database with the UTC.


Solution

  • To convert a local date to UTC in ColdFusion or Lucee:

    utcNow = dateConvert("Local2UTC", now())
    

    To insert it as a value in a database:

    queryExecute(sql="
        insert into tablename (
            utcDate
        ) values (
            :utcNow
        )
        ",
        params=[
            {name="utcNow", value=utcNow, cfsqltype="cf_sql_timestamp"}
        ],
        options={
            datasource: dsn,
            result: "local.insertResult"
        }
    );