APIs & Integrations

VNguyen0
Member

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

SOLVE

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 Upvotes
2 Accepted solutions
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

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

SOLVE

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

View solution in original post

0 Upvotes
Expertopinionsa
Solution
Participant

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

SOLVE

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.

View solution in original post

0 Upvotes
16 Replies 16
MaxWCS
Member

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

SOLVE

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 Upvotes
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

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

SOLVE

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 Upvotes
VNguyen0
Member

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

SOLVE

Thanks I'll give it a try.

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

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

SOLVE

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

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Expertopinionsa
Participant

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

SOLVE

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 Upvotes
VNguyen0
Member

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

SOLVE

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 Upvotes
Expertopinionsa
Solution
Participant

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

SOLVE

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 Upvotes
DPezzulo
Member

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

SOLVE

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 Upvotes
ALintermanns
Participant

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

SOLVE

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 Upvotes
MaxWCS
Member

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

SOLVE

Hi
juste do this 

 

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

 

0 Upvotes
ALintermanns
Participant

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

SOLVE

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 Upvotes
MaxWCS
Member

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

SOLVE

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

 

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

0 Upvotes
FSiddiqui8
Member

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

SOLVE

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

0 Upvotes
ALintermanns
Participant

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

SOLVE

Thanks a lot! It worked. 

0 Upvotes
LDeBenedetti
Participant

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

SOLVE

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

0 Upvotes
DPezzulo
Member

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

SOLVE

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 Upvotes