# Using code to compare date fields

**URL:** <https://community.hubspot.com/t/using-code-to-compare-date-fields/78798>\
**Category:** CMS Development\
**Tags:** local-development, javascript, enterprise\
**Created:** [April 5, 2023, 5:01pm UTC](https://community.hubspot.com/t/using-code-to-compare-date-fields/78798 "2023-04-05T17:01:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TSweet2](https://avatars.discourse-cdn.com/v4/letter/t/7cd45c/32.png) [@TSweet2](https://community.hubspot.com/u/TSweet2)\
**Post date:** [April 5, 2023, 5:01pm UTC](https://community.hubspot.com/t/using-code-to-compare-date-fields/78798/1 "2023-04-05T17:01:06Z")

</div>

Hello,

I am trying to write a custom code snippet in a workflow that would allow me to compare the create date to the close date of a deal to see if they’re equal. The reason for this is I would like to sort deals that have the same create and close date and have them be on a separate branch for the next step of the workflow. Any help regarding set-up or code editing would be appreciated. (For the API key I used the variable “API-Key” only for the example, the real API key comes from the private app)

Here is waht I have currently:

Language: node.js 16.x

Secret: Test

Properties:

createdate= Create date

closedate= Close date

Outputs:

Enumeration= hs\_execution\_state

API key: From private app - scope has: deal objects/ schema read/write, automation, actions, oauth

Code:

const hubspot = require(‘@hubspot/api-client’);

const hubspotClient = new hubspot.Client({ apiKey: ‘API-Key’ });

(async () =\> {  
try {  
// Initialize HubSpot API client  
await hubspotClient.cms.intialize();

// Define properties  
const createDate = “createdate”;  
const closeDate = “closedate”;

// Fetch deals  
const deals = await hubspotClient.crm.deals.getAll();  
// Loop through each deal  
for (const deal of deals.results) {  
// Extract create date and close date  
const createDate = deal.properties[createDate];  
const closeDate = deal.properties[closeDate];  
// Compare create date and close date  
if (createDate === closeDate) {  
// Create branch for deals where create date and close date are equal  
console.log(`Deal ${deal.id}: Create date and Close date are equal`);  
// … Create branch in HubSpot workflow for this deal  
await hubspotClient.crm.workflows.createBranch(deal.id, “equal\_date\_branch”);  
} else {  
// Create branch for deals where create date and close date are not equal  
console.log(`Deal ${deal.id}: Create date and Close date are not equal`);  
// … Create branch in HubSpot workflow for this deal  
await hubspotClient.crm.workflows.createBranch(deal.id, “not\_equal\_date\_branch”);  
}  
}  
} catch (error) {  
console.error(‘Error fetching deals from HubSpot:’, error);  
}  
})();

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [April 6, 2023, 8:23pm UTC](https://community.hubspot.com/t/using-code-to-compare-date-fields/78798/2 "2023-04-06T20:23:24Z")

</div>

Hi, @TSweet2 👋 Thanks for sharing your project and including those details. Hey, @stefen @Oezcan, do you have any suggestions for @TSweet2? Or other ways you might approach a project like this?

Thank you very much for taking a look.

Best,

Jaycee
