'Hubspot API Integration > Converting a date to UTC with a 12AM timestamp

Ok, this is driving me batty! Hubspot wants an ISO formatted UTC date and time string where the time is set to midnight UTC. In Coldfusion, no matter what I try, I get the error "INVALID_LONG" when passing a date to the HubSpot API.

I create a new date in Coldfusion using:

 dateobj =  CreateDateTime(
   year(regdate),
   month(regdate),
   day(regdate),
   '0','0','0'
)

This gives me a date object with midnight in the server timezone: {ts '2016-07-10 00:00:00'}

How do I convert that to an ISO formatted UTC datetime objectand keep the time at midnight Hubspot only cares about the date part, and wants the time to be midnight.

I've tried two UDFS (included below).. but when I use them:

local.dlib.dateToUTC(local.dlib.getIsoTimeString(dateobj))

I get "1468134000" which is not accepted by hubspot.

This is the same problem outlined here, and the JS code in that example works and the result "1608163200000" is accepted by Hubspot.

UDFS tried:

// I take the given date/time object and return the string that
// reprsents the date/time using the ISO 8601 format standard.
// The returned value is always in the context of UTC and therefore
// uses the special UTC designator ("Z"). The function will
// implicitly convert your date/time object to UTC (as part of
// the formatting) unless you explicitly ask it not to.
string function getIsoTimeString(
    required date datetime,
    boolean convertToUTC = true
    ) {
    if ( convertToUTC ) {
        datetime = dateConvert( "local2utc", datetime );
    }
    // When formatting the time, make sure to use "HH" so that the
    // time is formatted using 24-hour time.
    return(
        dateFormat( datetime, "yyyy-mm-dd" ) &
        "T" &
        timeFormat( datetime, "HH:mm:ss" ) &
        "Z"
    );

and

<cffunction name="dateToUTC" output="false" access="public" returntype="any" hint="Take a date and return the number of seconds since the Unix Epoch">
    <cfargument name="date" type="any" required="true" />
    <cfreturn dateDiff("s", dateConvert("utc2Local", "January 1 1970 00:00"), arguments.date) />
</cffunction>


Solution 1:[1]

Try using ParseDateTime() and specifying the time zone i.e. Z for UTC Time Zone. Then call getTime() on the returned date object.

// Result: 1649980800000
msSinceEpoch = parseDateTime( "2022-04-15Z", "yyyy-MM-ddX").getTime();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SOS