Search code examples
c#hubspothubspot-crm

Error posting date to HubSpot API


I am trying to post a date field to the HubSpot contact API, however, I am getting errors stating a UTC date is not midnight.

"1505779200 is at 10:16:19.200 UTC, not midnight!"

However, if you use this tool, and enter that value you will see that the value is midnight.

The c# code I use to do the conversion is:

 public static double DateTimeToUTC(System.DateTime dateTime)
 {
        dateTime = System.DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
        var utcValue = ((DateTimeOffset) dateTime).ToUnixTimeSeconds();
        return utcValue;
 }

Can anyone help?

Cheers

KH


Solution

  • Have you tried posting the UNIX time in milliseconds (rather than seconds)? HubSpot FAQ here

    HubSpot API endpoints accept UNIX formatted timestamps in milliseconds. ...

    ...

    Date/Datetime properties in HubSpot Contacts

    Date properties will only store the date, and must be set to midnight UTC for the date you want. For example, May 1 2015 would be 1430438400000 (01 May 2015 00:00:00 UTC). If you try to set a value that is not midnight UTC, you will receive an error.

    Try switching to .toUnixTimeMilliseconds() and give it a whirl! (MSDN Ref)

    public static double DateTimeToUTC(System.DateTime dateTime)
    {
        dateTime = System.DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
        var utcValue = ((DateTimeOffset) dateTime).ToUnixTimeMilliseconds();
        return utcValue;
    }