I’m having trouble with the HubSpot API. It needs a UTC date in ISO format set to midnight. But my ColdFusion code keeps getting an “INVALID_LONG” error.
Here’s what I’ve tried:
myDate = CreateDateTime(
year(originalDate),
month(originalDate),
day(originalDate),
0, 0, 0
)
This gives me {ts ‘2016-07-10 00:00:00’} in the server’s timezone. But how do I make it UTC and keep it at midnight?
I also tried some custom functions:
formattedDate = myDateUtils.formatIsoDate(myDateUtils.convertToUtcSeconds(myDate))
This returns “1468134000”, but HubSpot doesn’t like it.
Someone else had a similar issue and fixed it with JavaScript. Their solution “1608163200000” works fine.
Any ideas on how to get this right in ColdFusion? HubSpot only cares about the date, but insists on midnight UTC time. I’m stuck!
Having worked with the HubSpot API before, I can relate to your frustration. One approach that’s worked well for me is leveraging Java’s built-in functionality within ColdFusion. Try this:
javaDate = CreateObject('java', 'java.util.Date').init(
year(originalDate) - 1900,
month(originalDate) - 1,
day(originalDate)
)
utcTimestamp = javaDate.getTime()
formattedDate = DateTimeFormat(utcTimestamp, 'yyyy-mm-dd''T''00:00:00''Z''', 'UTC')
This should give you a properly formatted UTC midnight timestamp that HubSpot will accept. The key is using the ‘UTC’ timezone in DateTimeFormat to ensure it’s truly UTC. If you still encounter issues, double-check HubSpot’s API documentation for any specific formatting requirements they might have.
I’ve dealt with similar issues when integrating ColdFusion with APIs that require specific date formats. Here’s what worked for me:
Try using the DateConvert() function to convert your local time to UTC, then format it with DateFormat() and TimeFormat(). Something like this:
utcDate = DateConvert('local2UTC', CreateDateTime(year(originalDate), month(originalDate), day(originalDate), 0, 0, 0))
formattedDate = DateFormat(utcDate, 'yyyy-mm-dd') & 'T' & TimeFormat(utcDate, 'HH:mm:ss') & 'Z'
This should give you a string in the format ‘2016-07-10T00:00:00Z’, which is typically what APIs expect for ISO 8601 UTC midnight.
If HubSpot specifically needs milliseconds since epoch, you might need to use Java’s Calendar class:
cal = CreateObject('java', 'java.util.Calendar').getInstance()
cal.setTime(utcDate)
milliseconds = cal.getTimeInMillis()
This should provide you with a long value similar to the JavaScript solution that worked. Hope this helps!