APIs & Integrations

VNguyen0
Miembro

String date to date property in automation/workflow with custom code

resolver

Trying to convert a string (mm/dd/yy) to something HubSpot won't bark at.

 

My code:

 

exports.main = async (event, callback) => {

var ca_date = event.inputFields['ca_date'];
ca_date = ca_date.split(' ').join('T');
console.log(ca_date);
var date = new Date(ca_date);
console.log('<br />' + date);
date = date.getTime() / 1000;
console.log('<br />' + date);

callback({
outputFields: {
date: date
}
});
}

 

date is being output as datetime, tested the code, example output below:

 

Screen Shot 2023-04-17 at 9.01.10 PM.png

and it seems to work, however when I try to use the "Copy Property Value" function within the workflow (to a datepicker property), all I get is this:

 

Event
  • Unable to update property because property value isn't valid

 

Tried to talk to HupSpot online support but I don't think they have the expertise that I'm looking for, they wanted me to export to excel then import, which defeats to purpose of a workflow/automation.

 

I've tried it with python also and have been racking my head trying to get this to work.

 

Any assistance from anyone would be greatly appreciated.

0 Me gusta
2 Soluciones aceptadas
Kevin-C
Solución
Experto reconocido | Partner
Experto reconocido | Partner

String date to date property in automation/workflow with custom code

resolver

Hey @VNguyen0 

 

You might just be over thinking it! Have you tried the .parse() method?

 

 

var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);

 

 

 

This should output a unixtimestamp in milliseconds, which is compatable with HS. You may want to do alittle additional modifiation if the time is important:

 

var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);
// set time to midnight to be compatable with HS date property
const midnightUnix = unixTimeStamp.setHours(0,0,0); // date.setHours(hours,minutes,seconds);
console.log(midnightUnix);

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

Ver la solución en mensaje original publicado

0 Me gusta
Expertopinionsa
Solución
Participante

String date to date property in automation/workflow with custom code

resolver

Really sorry for the confusion. It looks like HubSpot's custom code function for workflows may not support the moment.js library. In that case, you can try using JavaScript's built-in Date object to convert the string date to an ISO 8601 format.


try this: 

exports.main = async (event, callback) => {
  const ca_date = event.inputFields['ca_date'];
  const date = new Date(ca_date);
  const isoDate = date.toISOString();
  console.log(isoDate);
  callback({
    outputFields: {
      date: isoDate
    }
  });
}

 

Here we're creating a new Date object from the string date and then using the Date object's toISOString() method to format it as an ISO 8601 string. You can then set the isoDate variable as the output field value, which should be in the correct format for HubSpot.

Ver la solución en mensaje original publicado

0 Me gusta
16 Respuestas 16
MaxWCS
Participante

String date to date property in automation/workflow with custom code

resolver

Hello,

 

Here is the good solution :

 

exports.main = async (event, callback) => { 
const date = event.inputFields['date']; 
var sdateconvert = new Date(sdate).valueOf(); 

callback({ 
outputFields: 
{ 
sdateconvert: sdateconvert 
} 
}); 
}

 

You just need to convert in unix timestamp in milliseconds.

 

Cheers,

0 Me gusta
Kevin-C
Solución
Experto reconocido | Partner
Experto reconocido | Partner

String date to date property in automation/workflow with custom code

resolver

Hey @VNguyen0 

 

You might just be over thinking it! Have you tried the .parse() method?

 

 

var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);

 

 

 

This should output a unixtimestamp in milliseconds, which is compatable with HS. You may want to do alittle additional modifiation if the time is important:

 

var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);
// set time to midnight to be compatable with HS date property
const midnightUnix = unixTimeStamp.setHours(0,0,0); // date.setHours(hours,minutes,seconds);
console.log(midnightUnix);

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Me gusta
VNguyen0
Miembro

String date to date property in automation/workflow with custom code

resolver

Thanks I'll give it a try.

0 Me gusta
Kevin-C
Experto reconocido | Partner
Experto reconocido | Partner

String date to date property in automation/workflow with custom code

resolver

Datetime can get complicated fast…way too fast IMO! Try to keep it as simple as possible!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Me gusta
Expertopinionsa
Participante

String date to date property in automation/workflow with custom code

resolver

Hi, it looks like the code you provided is converting the string date to a Unix timestamp (in seconds), which may not be compatible with HubSpot's date format. HubSpot expects a date value in ISO 8601 format, which is "yyyy-mm-ddThh:mm:ssZ".

 

You can modify your code to convert the string date to the ISO 8601 format before setting it as the output field value.

 

Here's an example implementation using the moment.js library:

 

const moment = require('moment');

exports.main = async (event, callback) => {
  const ca_date = event.inputFields['ca_date'];
  const date = moment(ca_date, 'MM/DD/YY').format('YYYY-MM-DDTHH:mm:ssZ');
  console.log(date);
  callback({
    outputFields: {
      date: date
    }
  });
}

 

 

 

In this implementation, we're using moment.js to parse the string date with the 'MM/DD/YY' format and format it as an ISO 8601 string using 'YYYY-MM-DDTHH:mm:ssZ'. You can then set the date variable as the output field value, which should be in the correct format for HubSpot. We always work in this manner at Triotech Systems. I hope this helps!

0 Me gusta
VNguyen0
Miembro

String date to date property in automation/workflow with custom code

resolver

Thanks for the tip. Unfortunately I couldn't get it to work. I'm using the custom code function in the workflow (node.js) and it complaining about moment.

 

I'll keep plugging away at it, just very frustrating as HubSpot hasn't provided any guidance. I'm able to use Zapier and use the formatter feature to do what it needs to do, but am trying to convert my zaps to workflows if possible.

 

0 Me gusta
Expertopinionsa
Solución
Participante

String date to date property in automation/workflow with custom code

resolver

Really sorry for the confusion. It looks like HubSpot's custom code function for workflows may not support the moment.js library. In that case, you can try using JavaScript's built-in Date object to convert the string date to an ISO 8601 format.


try this: 

exports.main = async (event, callback) => {
  const ca_date = event.inputFields['ca_date'];
  const date = new Date(ca_date);
  const isoDate = date.toISOString();
  console.log(isoDate);
  callback({
    outputFields: {
      date: isoDate
    }
  });
}

 

Here we're creating a new Date object from the string date and then using the Date object's toISOString() method to format it as an ISO 8601 string. You can then set the isoDate variable as the output field value, which should be in the correct format for HubSpot.

0 Me gusta
DPezzulo
Miembro

String date to date property in automation/workflow with custom code

resolver

I'm trying to do this and I'm struggling with writing the properties still.  

I get outputs that look exactly like the format (from what I can tell):

DPezzulo_0-1690518288044.png
Why are these coming back as invalid for datepicker fields?

 

 

0 Me gusta
ALintermanns
Participante

String date to date property in automation/workflow with custom code

resolver

Hi! Did you manage to resolve this issue? I am facing the same and I am not able to find out what the problem is. 

0 Me gusta
MaxWCS
Participante

String date to date property in automation/workflow with custom code

resolver

Hi
juste do this 

 

var convertDate = new Date(yourDate).valueOf();

 

0 Me gusta
ALintermanns
Participante

String date to date property in automation/workflow with custom code

resolver

Thank you. I am not sure if my reply shows correctly, but I have the output correct in ISO timestamp. But I am not able to pass it to property. As it shows "invalid data" 

 

Would the code you added correct this issue? 

Screenshot 2024-02-15 at 10.21.48.png

 

Screenshot 2024-02-15 at 09.35.39.png

 

My issue is in this step: 

Screenshot 2024-02-15 at 10.21.27.png

The delivery date (timestamp) is a date picker property. 

Screenshot 2024-02-15 at 10.21.19.png

 

Hope you can help me, I am been 3 weeks stuck with this. 

 

Thank you!!

 

0 Me gusta
MaxWCS
Participante

String date to date property in automation/workflow with custom code

resolver

Please read my comment, just convert your date with valueOf();

 

You need to have a milliseconds result like this : check here 

0 Me gusta
FSiddiqui8
Miembro

String date to date property in automation/workflow with custom code

resolver

I am facing the same issue. Tried your solution using the valueOf() however couldnt figure out why it still isn't working.

0 Me gusta
ALintermanns
Participante

String date to date property in automation/workflow with custom code

resolver

Thanks a lot! It worked. 

0 Me gusta
LDeBenedetti
Participante

String date to date property in automation/workflow with custom code

resolver

Hi! Have you managed to sort this? I'm after the same issue. Thanks!

0 Me gusta
DPezzulo
Miembro

String date to date property in automation/workflow with custom code

resolver

Yes, the solution for me was to have the output come through as unix epoch timestamp, even though it seems to imply it wants a datetime.  

It needs to be in miliseconds, here's an example of a working output value:
1689897600000

0 Me gusta