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

**URL:** <https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501>\
**Category:** APIs & Integrations\
**Tags:** python, workflows\
**Created:** [April 18, 2023, 1:04am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501 "2023-04-18T01:04:16Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![VNguyen0](https://avatars.discourse-cdn.com/v4/letter/v/e19adc/32.png) [@VNguyen0](https://community.hubspot.com/u/VNguyen0)\
**Post date:** [April 18, 2023, 1:04am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/1 "2023-04-18T01:04:16Z")

</div>

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](https://d2qpr2jzcazej7.cloudfront.net/original/3X/3/2/3232642d520ffafdf25e212071a6b66e269a2e1c.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.

---

<div class="post-metadata">

**Author:** ![Expertopinionsa](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@Expertopinionsa](https://community.hubspot.com/u/Expertopinionsa)\
**Post date:** [April 18, 2023, 8:44am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/2 "2023-04-18T08:44:50Z")

</div>

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:

```lia-code-sample
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!

---

<div class="post-metadata">

**Author:** ![VNguyen0](https://avatars.discourse-cdn.com/v4/letter/v/e19adc/32.png) [@VNguyen0](https://community.hubspot.com/u/VNguyen0)\
**Post date:** [April 18, 2023, 3:23pm UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/3 "2023-04-18T15:23:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Expertopinionsa](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@Expertopinionsa](https://community.hubspot.com/u/Expertopinionsa)\
**Post date:** [April 20, 2023, 10:19am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/4 "2023-04-20T10:19:53Z")

</div>

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:

```lia-code-sample
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.

---

<div class="post-metadata">

**Author:** ![DPezzulo](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DPezzulo](https://community.hubspot.com/u/DPezzulo)\
**Post date:** [July 28, 2023, 4:25am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/5 "2023-07-28T04:25:35Z")

</div>

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](https://d2qpr2jzcazej7.cloudfront.net/original/3X/6/f/6f1878f2526b7c3e4e8dca5d69799437032d3146.png)

Why are these coming back as invalid for datepicker fields?

---

<div class="post-metadata">

**Author:** ![LDeBenedetti](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@LDeBenedetti](https://community.hubspot.com/u/LDeBenedetti)\
**Post date:** [October 18, 2023, 2:57pm UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/6 "2023-10-18T14:57:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DPezzulo](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DPezzulo](https://community.hubspot.com/u/DPezzulo)\
**Post date:** [October 18, 2023, 4:19pm UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/7 "2023-10-18T16:19:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ALintermanns](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@ALintermanns](https://community.hubspot.com/u/ALintermanns)\
**Post date:** [February 15, 2024, 8:16am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/8 "2024-02-15T08:16:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MaxWCS](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@MaxWCS](https://community.hubspot.com/u/MaxWCS)\
**Post date:** [February 15, 2024, 8:33am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/9 "2024-02-15T08:33:47Z")

</div>

Hi  
juste do this

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

---

<div class="post-metadata">

**Author:** ![ALintermanns](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@ALintermanns](https://community.hubspot.com/u/ALintermanns)\
**Post date:** [February 15, 2024, 9:23am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/10 "2024-02-15T09:23:18Z")

</div>

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](https://d2qpr2jzcazej7.cloudfront.net/original/3X/6/b/6b0b9f164a0918ddaeb926ccf79f6263e008b151.png)

 ![Screenshot 2024-02-15 at 09.35.39.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/d/5d9930af59a1b2b3ca90911dc9189c8ff4b70536.png)

My issue is in this step:

 ![Screenshot 2024-02-15 at 10.21.27.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/4/4ca002b70511ac531fca732a6b0d1800f4ff785c.png)

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

 ![Screenshot 2024-02-15 at 10.21.19.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/2/42df2d3ed54e34301de2e893d5dc91634b7689a5.png)

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

Thank you!!

---

<div class="post-metadata">

**Author:** ![MaxWCS](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@MaxWCS](https://community.hubspot.com/u/MaxWCS)\
**Post date:** [February 15, 2024, 9:27am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/11 "2024-02-15T09:27:57Z")

</div>

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

You need to have a milliseconds result like this : [check here](https://www.geeksforgeeks.org/javascript-date-valueof-method/#:~:text=Date.valueOf()%20method.-,Example%201%3A%C2%A0,-javascript)

---

<div class="post-metadata">

**Author:** ![ALintermanns](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@ALintermanns](https://community.hubspot.com/u/ALintermanns)\
**Post date:** [February 15, 2024, 9:48am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/12 "2024-02-15T09:48:58Z")

</div>

Thanks a lot! It worked.

---

<div class="post-metadata">

**Author:** ![FSiddiqui8](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@FSiddiqui8](https://community.hubspot.com/u/FSiddiqui8)\
**Post date:** [March 29, 2024, 12:29am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/13 "2024-03-29T00:29:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [April 18, 2023, 7:08pm UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/14 "2023-04-18T19:08:06Z")

</div>

Hey @VNguyen0

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

```lia-code-sample
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:

```lia-code-sample
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);

```

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [April 18, 2023, 7:15pm UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/15 "2023-04-18T19:15:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![VNguyen0](https://avatars.discourse-cdn.com/v4/letter/v/e19adc/32.png) [@VNguyen0](https://community.hubspot.com/u/VNguyen0)\
**Post date:** [April 18, 2023, 9:57pm UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/16 "2023-04-18T21:57:51Z")

</div>

Thanks I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![MaxWCS](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@MaxWCS](https://community.hubspot.com/u/MaxWCS)\
**Post date:** [November 15, 2023, 8:44am UTC](https://community.hubspot.com/t/string-date-to-date-property-in-automation-workflow-with-custom-code/79501/17 "2023-11-15T08:44:13Z")

</div>

Hello,

Here is the good solution :

```lia-code-sample
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,
