# Custom Code to change Date/Time stamp to Date

**URL:** <https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875>\
**Category:** 💬 RevOps Discussions\
**Tags:** admin, data-quality-command-center, revops, salesops\
**Created:** [June 21, 2024, 7:03pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875 "2024-06-21T19:03:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lana604](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lana604](https://community.hubspot.com/u/lana604)\
**Post date:** [June 21, 2024, 7:03pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/1 "2024-06-21T19:03:24Z")

</div>

I have a workflow rule where I grab a date/time field with custom code and update that field to a date field (MM/DD/YYYY) as an output.  
Example:  
Date/time: 2024-06-21T00:29:23.243Z  
Date: 06/21/2024  
I receive a “Success” message when testing the custom code. However, when I copy the output date and input it into my “signup\_date” field I receive a message saying “Unable to update property because property value isn’t valid.” I’ve played around with the format of the output date to YYYY/MM/DD and DD/MM/YYYY as well as with “-” instead of “/” … but no luck! 😞  
How can I fix this?

 ![2024-06-21_11-46-15.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/b/cb38a6bb1c30aa16b4a8603e3dc4262505399f93.png)  
Custom Code  
 ![2024-06-21_11-46-33.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/4/c4672ed63ea7d2e1734ca02b6a2d74542b441c9a.png)  
Success Message from Code  
 ![2024-06-21_11-48-23.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/8/c8a2825cc0df01fa49d35e88103b03d3cf5f2df7.png)  
Workflow  
 ![2024-06-21_11-35-34.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/c/cc1eeeeb2c9d48dfb66997dc429b2a53c589799b.png)  
Error Message

---

<div class="post-metadata">

**Author:** ![RaymondThomas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/raymondthomas/32/68925_2.png) [@RaymondThomas](https://community.hubspot.com/u/RaymondThomas)\
**Post date:** [June 22, 2024, 1:42am UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/2 "2024-06-22T01:42:30Z")

</div>

What is the field type for that signup\_date custom field?

---

<div class="post-metadata">

**Author:** ![RaymondThomas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/raymondthomas/32/68925_2.png) [@RaymondThomas](https://community.hubspot.com/u/RaymondThomas)\
**Post date:** [June 22, 2024, 1:45am UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/3 "2024-06-22T01:45:50Z")

</div>

That and check for property validation rules

---

<div class="post-metadata">

**Author:** ![lana604](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lana604](https://community.hubspot.com/u/lana604)\
**Post date:** [June 24, 2024, 5:45pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/4 "2024-06-24T17:45:52Z")

</div>

It’s a Date Picker field. My date format is the United States, so MM/DD/YYYY.

 ![lana604_0-1719251035563.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/b/b0c5d28750a805b66545dac6068cd94aa8ae0653.png)

I do not have any validation rules associated to the field.

 ![lana604_1-1719251141158.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/c/5c6749ff16e37cc5e4b41946b5894ee243fcfbd9.png)

---

<div class="post-metadata">

**Author:** ![SteveHTM](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stevehtm/32/22074_2.png) [@SteveHTM](https://community.hubspot.com/u/SteveHTM)\
**Post date:** [June 24, 2024, 8:15pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/5 "2024-06-24T20:15:24Z")

</div>

@lana604 - based on my own efforts on this topic, the only acceptable format for a date output from a custom code workflow is a timestamp value in milliseconds - not a formatted date string as you might expect. As in 1708905600000 rather than 2024-02-26.

Hope this helps!

Steve

---

<div class="post-metadata">

**Author:** ![lana604](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lana604](https://community.hubspot.com/u/lana604)\
**Post date:** [June 25, 2024, 8:34pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/6 "2024-06-25T20:34:18Z")

</div>

Hi Steve,  
I updated the output code to time stamp value in milliseconds, unfortunately, when I copy the property value to my date field, I get the same error where “property value is not valid…”

I’m thinking this might be a HubSpot limitation. I have developers looking into this but so far no luck .

---

<div class="post-metadata">

**Author:** ![LeanData-TonyJ](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@LeanData-TonyJ](https://community.hubspot.com/u/LeanData-TonyJ)\
**Post date:** [November 14, 2024, 7:28pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/7 "2024-11-14T19:28:34Z")

</div>

@lana604 Did they ever get back to you? I am running into the same exact error. I’ve tried date fields, datetime fields, and nothing will accept the millisecond time stamp

---

<div class="post-metadata">

**Author:** ![svamann](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/svamann/32/77496_2.png) [@svamann](https://community.hubspot.com/u/svamann)\
**Post date:** [November 15, 2024, 11:46am UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/8 "2024-11-15T11:46:09Z")

</div>

Did you ensure the timestamp is returned as int? Because if it’s a string in the background, subsequent actions won’t accept it… see [my solution above](https://community.hubspot.com/t5/128172-RevOps-Discussions/Custom-Code-to-change-Date-Time-stamp-to-Date/m-p/1046272/highlight/true#M2699).

---

<div class="post-metadata">

**Author:** ![LeanData-TonyJ](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@LeanData-TonyJ](https://community.hubspot.com/u/LeanData-TonyJ)\
**Post date:** [November 15, 2024, 5:33pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/9 "2024-11-15T17:33:26Z")

</div>

I tried this as well. I have also tried almost every other output I could think of. However, I think the issue could be with the new consolidation of record updates on workflows. Since Clear, Copy, and Update are now consilidated into one action. Here is my code, any help would be very much appreciated.

```lia-code-sample
exports.main = async (event, callback) => {
 try {
 const contactId = event.object.objectId;
 const accessToken = process.env.PRIVATE_APP_ACCESS_TOKEN; // Access your secret here

 // Fetch associated deals for the contact
 const associationsResponse = await fetch(`https://api.hubapi.com/crm/v3/objects/contacts/${contactId}/associations/deals`, {
 method: 'GET',
 headers: {
 Authorization: `Bearer ${accessToken}`,
 'Content-Type': 'application/json'
 }
 });

 const associationsData = await associationsResponse.json();
 console.log("Associations Data:", associationsData);

 if (!associationsData.results) {
 throw new Error("No associated deals found");
 }

 const dealIds = associationsData.results.map(deal => deal.id);

 // Fetch details for each associated deal
 const dealDetailsPromises = dealIds.map(dealId => 
 fetch(`https://api.hubapi.com/crm/v3/objects/deals/${dealId}`, {
 method: 'GET',
 headers: {
 Authorization: `Bearer ${accessToken}`,
 'Content-Type': 'application/json'
 },
 params: {
 properties: 'dealname,createdate'
 }
 })
 );

 const dealDetailsResponses = await Promise.all(dealDetailsPromises);
 const dealDetails = await Promise.all(dealDetailsResponses.map(res => res.json()));

 // Extract deal names and set create dates to midnight UTC
 const dealsInfo = dealDetails.map(deal => {
 const createDate = new Date(deal.properties.createdate);
 createDate.setUTCHours(0, 0, 0, 0); // Ensure midnight UTC
 return {
 name: deal.properties.dealname,
 createDate
 };
 });

 console.log("Deals Info:", dealsInfo);

 // Find the most recent deal create date
 const mostRecentDeal = dealsInfo.reduce((latest, current) => 
 current.createDate > latest.createDate ? current : latest
 );

 const timestamp = mostRecentDeal.createDate.getTime();
 const isoString = mostRecentDeal.createDate.toISOString();
 const formattedDate = `${(mostRecentDeal.createDate.getMonth() + 1).toString().padStart(2, '0')}/${mostRecentDeal.createDate.getDate().toString().padStart(2, '0')}/${mostRecentDeal.createDate.getFullYear()}`;
 const formattedDateV2 = `${mostRecentDeal.createDate.getFullYear()}-${(mostRecentDeal.createDate.getMonth() + 1).toString().padStart(2, '0')}-${mostRecentDeal.createDate.getDate().toString().padStart(2, '0')}`;

 console.log("Most Recent Deal:", mostRecentDeal);
 console.log("Timestamp:", timestamp);
 console.log("ISO String:", isoString);
 console.log("Formatted Date:", formattedDate);
 console.log("Formatted Date:", formattedDateV2);

 // Return the most recent deal name and create date in various formats
 callback({
 outputFields: {
 associatedDealNames: mostRecentDeal.name,
 mostRecentDealCreateDate: timestamp, // Use timestamp
 mostRecentDealCreateDateISO: isoString, // Use ISO string
 mostRecentDealCreateDateFormatted: formattedDateV2, // Use formatted date
 mostRecentDealCreateDateV2: parseInt(timestamp), // Use timestamp
 mostRecentDealCreateTimeStamp: Math.floor(timestamp),
 mostRecentDealDateNumber: parseInt(timestamp)
 }
 });
 } catch (error) {
 console.error("Error:", error.message);
 callback({
 outputFields: {
 associatedDealNames: "Error fetching deals",
 mostRecentDealCreateDate: "Error fetching date",
 mostRecentDealCreateDateISO: "Error fetching date",
 mostRecentDealCreateDateFormatted: "Error fetching date"
 }
 });
 }
};

```

 ![LeanDataTonyJ_2-1731691928969.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/2/8/288c4fdabb988f29dbd7358d8ab54346bd77ac10.png)

 ![LeanDataTonyJ_0-1731691770633.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/9/914e89b254f7276aefbbfb5dc24e24c7deebb77b.png)

 ![LeanDataTonyJ_1-1731691846757.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/1/51b4e02074dca108ce80247b3c028522c3dd4307.png)

---

<div class="post-metadata">

**Author:** ![lana604](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lana604](https://community.hubspot.com/u/lana604)\
**Post date:** [November 20, 2024, 1:54am UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/10 "2024-11-20T01:54:37Z")

</div>

Hi @LeanData-TonyJ. You must set the custom code to update the date/time field and set it to UNIX milliseconds at midnight.

---

<div class="post-metadata">

**Author:** ![Bortami](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bortami/32/221350_2.png) [@Bortami](https://community.hubspot.com/u/Bortami)\
**Post date:** [June 29, 2024, 3:37am UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/11 "2024-06-29T03:37:40Z")

</div>

The date must best set to midnight in order to copy via native workflows.

Here are my tests:

 ![Screenshot 2024-06-28 at 10.33.24 PM.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/5/456bc2edc394a80aeaabcb9ade6f3594c4aa287d.jpeg)  
code used

 ![Screenshot 2024-06-28 at 10.33.28 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/e/ae9ee5d414be49da27c7eecd13ed76fd684e61c7.png)  
output defined as datetime  
 ![Screenshot 2024-06-28 at 10.34.25 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/5/552e7dcaa824a79c1abc43c9f95ef64f4b60c0a8.png)  
in milliseconds with time set to midnight  
 ![Screenshot 2024-06-28 at 10.28.23 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/a/4aa618b2518da18cb04d85ff09829d11c8309907.png)  
string set to midnight

 ![Screenshot 2024-06-28 at 10.39.53 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/e/1/e11bbe6e943ed9c8a58fb3f30bc9bea7d52ad3ef.png)

---

<div class="post-metadata">

**Author:** ![lana604](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lana604](https://community.hubspot.com/u/lana604)\
**Post date:** [July 2, 2024, 5:41pm UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/12 "2024-07-02T17:41:29Z")

</div>

Thank you Bortami, this did the trick!  
To summarize to copy the date time fields to a date field, the custom code must change the date/time field to set to UNIX milliseconds at midnight.

---

<div class="post-metadata">

**Author:** ![svamann](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/svamann/32/77496_2.png) [@svamann](https://community.hubspot.com/u/svamann)\
**Post date:** [September 26, 2024, 9:15am UTC](https://community.hubspot.com/t/custom-code-to-change-date-time-stamp-to-date/109875/13 "2024-09-26T09:15:28Z")

</div>

Ran into the same issue. With Python custom code. Got it to work by formatting the date as a timestamp in milliseconds AND ensuring it is an integer (rather than any kind of floating point number, I suppose):

![](https://d2qpr2jzcazej7.cloudfront.net/optimized/3X/c/e/cee2ce1ba7c4f0f5676063db68e89bd1da0aa27b_2_690x22.png)

Response type of the custom code is set to “Date”:

 ![Screenshot 2024-09-26 at 11.13.22.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/8/1/8178ee4b7d2ae1e3e6be3e21d57b4ce5e69d42c9.png)

Output now looks like this and is accepted by a “Copy property” action writing to a DatePicker field.

![](https://d2qpr2jzcazej7.cloudfront.net/optimized/3X/9/6/9613c61facd591e53568e3c7faa2aa73248748f3_2_690x29.png)
