APIs & Integrations

EShumely
Participant | Diamond Partner
Participant | Diamond Partner

passing timestamp variable through API.

SOLVE

I'm pulling the data from a google sheet. 

this is an example of a value - 10/4/2021 16:10:23

How do I pass this as a date? I haven't done this before so I have no experience with converting dates.  the examples I found weren't working for me. 

can you provide the right function for me to get the right date format?

 

Thanks. (I read HubSpot page about it, bcz I have no experience with dates the info they provided didn't help) 

0 Upvotes
1 Accepted solution
EShumely
Solution
Participant | Diamond Partner
Participant | Diamond Partner

passing timestamp variable through API.

SOLVE

@piersg that's similar to what I was thinking. if I pass just the date the hours will be midnight by default.  but it wasn't the case. even when I chose the hours as 0,0,0,0 it still wasn't the right hours. finally today I got the answer. 

there is a method called 

setUTCHours and you need to use it. 
var date = new Date(timestemp).setUTCHours(0000);

View solution in original post

6 Replies 6
piersg
Key Advisor

passing timestamp variable through API.

SOLVE

@EShumely If you want to convert that date (including the time) directly in to a unix timestamp in JS, you could do this

 

var date = '10/4/2021 16:10:23'
var unixDate = Math.floor(new Date(date).getTime() / 1000);
console.log(unixDate);
// returns "1633360223"

 

 

To follow up on @davidoff2drew's suggestion, if the data from Google is always a string in the format "10/4/2021 16:10:23" then you could use the HubL split filter to remove the time. e.g.

 

{% set dateAndTime = "10/4/2021 16:10:23" %}
{% set date = dateAndTime|split(' ') %}
{{date[0]}}

 

 

In JS, you can split a string like this:

 

var date = '10/4/2021 16:10:23'
console.log(date.split(" ")[0]);
// returns "10/4/2021"

 

 

EShumely
Solution
Participant | Diamond Partner
Participant | Diamond Partner

passing timestamp variable through API.

SOLVE

@piersg that's similar to what I was thinking. if I pass just the date the hours will be midnight by default.  but it wasn't the case. even when I chose the hours as 0,0,0,0 it still wasn't the right hours. finally today I got the answer. 

there is a method called 

setUTCHours and you need to use it. 
var date = new Date(timestemp).setUTCHours(0000);
EShumely
Participant | Diamond Partner
Participant | Diamond Partner

passing timestamp variable through API.

SOLVE

  Thanks 🙂 have you tried your solution? the value on my google sheet look like time  - 10/7/2021 8:07:39

when I log this value it looks like this  - 

Mon Jul 12 15:32:28 GMT+03:00 2021
I couldn't use the split function on that so I used to toLocalString so would be able to get only the date.
I assumed  if I pass only the date string to this 
var unixDate = Math.floor(new Date(date).getTime() / 1000);​

I will get the right value. but it still gave me this error - > "1626037200 is at 19:40:37.200 UTC, not midnight!"

Any thoughts? I must be missing something. it is driving me crazy 

 

0 Upvotes
EShumely
Participant | Diamond Partner
Participant | Diamond Partner

passing timestamp variable through API.

SOLVE

@dennisedson  Thanks 🙂 I'm using js. I've tried everything I could find. Thanks for the reference but I tried that too. tried this link as well https://www.epochconverter.com/

I think my lack of understanding of this object makes something very easy difficult for me. 

firstly I don't know what date am I getting, I don't know what time zone it is. I don't know how to convert it to UTC  (thought I found something but it didn't make any difference) I ended up using the error msg to get my UTC time and just hardcode the difference. but I don't need to mention this is a horrible solution. 

 

@davidoff2drew  Hi David, I don't like not being able to do something more than that, I don't like being told that something can't be done (someone from my team told me it's not possible so now I am invested).

as for passing it as string, how would you filter based on a date if you passed them as string? 

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

passing timestamp variable through API.

SOLVE

@EShumely 

You will need to convert to unix timestamp in milliseconds

How you do it depends on what language you are using, but for a quick POC. you can use this site

@DanielSanchez , @piersg might have some advice 😀

davidoff2drew
Contributor | Elite Partner
Contributor | Elite Partner

passing timestamp variable through API.

SOLVE

I wouldn't be able to give you a function without knowing how you're building what you're building. But, you can either set the time to 00:00:00 (or whatever equals UTC midnight for you), or remove the time part of the string and just send in the date string.

0 Upvotes