⚙ Operations Hub

Shuler
Participant

Calculate difference between start date and end date in a custom object

SOLVE

I have created a custom object in HubSpot, which has a start and end date - the calculated field can check for the time between however we run into an issue where if it's the same day, we get a 0 value when we would want to see a 1 value.

 

I've tried to do some custom code via a workflow to calculate this, but I am not a programmer and have no idea how to get something like this to work. What I am trying to do is if the start date and end day is the same, make the field say 1, if they are not the same dates, then calculate the difference.

 

Any help into this would be greatly appreciated. 

0 Upvotes
2 Accepted solutions
Shuler
Solution
Participant

Calculate difference between start date and end date in a custom object

SOLVE

Thanks @MiaSrebrnjak and @ankitparmar09 - I wanted to share that I came up with a solution and it works quite well to what I need.

Via the API, I created a calculation field with an if then condition. It calculates the difference between the times and if they are 0, it will output 1, otherwise I do the calculation, divide it by the miliseconds and get days. It just outputs a number, but this is exaclty what we need and want. Code below:

 

{
  "name": "duration_days",
  "label": "Duration Days",
  "type": "number",
  "fieldType": "calculation_equation",
  "groupName": "pipeline_milestones",
  "calculationFormula": "if end_date-start_date=0 then 1 else (end_date-start_date)/86400000"
  }

 

This field then shows up just calculating the difference in days, and we're all set to report on it 🙂  

View solution in original post

GreyHamilton
Solution
Participant | Elite Partner
Participant | Elite Partner

Calculate difference between start date and end date in a custom object

SOLVE

@Shuler Love this solution. I believe yours is the ideal solution, but for anyone who isn't comfortable with APIs or custom code I figured out a way to do it with a 'Days Between' calculated field and the Format feature in a workflow.

Here is the workflow utilizing the Format function. I tagged this as "Original Duration" as you could have it not re-enroll so it's only set once. Depending on if your dates change, you could potentially benefit from a "snapshot" of the original duration. The round was added because it was adding a 0 decimal point even though the property limited them.

GreyHamilton_0-1680554288647.png

 

Results: 

GreyHamilton_1-1680554634513.png

 

Did this post help solve your problem? If so, please mark it as a solution.

Grey Hamilton, Sr. Technical Consultant
Bayard Bradford

Advanced CRM Implementation | HubSpot Custom Integration Services | Datawarehouse.io Apps for HubSpot
Elite HubSpot Solutions Partner
> Follow me on LinkedIn

View solution in original post

5 Replies 5
Shuler
Solution
Participant

Calculate difference between start date and end date in a custom object

SOLVE

Thanks @MiaSrebrnjak and @ankitparmar09 - I wanted to share that I came up with a solution and it works quite well to what I need.

Via the API, I created a calculation field with an if then condition. It calculates the difference between the times and if they are 0, it will output 1, otherwise I do the calculation, divide it by the miliseconds and get days. It just outputs a number, but this is exaclty what we need and want. Code below:

 

{
  "name": "duration_days",
  "label": "Duration Days",
  "type": "number",
  "fieldType": "calculation_equation",
  "groupName": "pipeline_milestones",
  "calculationFormula": "if end_date-start_date=0 then 1 else (end_date-start_date)/86400000"
  }

 

This field then shows up just calculating the difference in days, and we're all set to report on it 🙂  

MiaSrebrnjak
Community Manager
Community Manager

Calculate difference between start date and end date in a custom object

SOLVE

That's great, thank you for sharing @Shuler!

 

And thank you for your help @GreyHamilton and @ankitparmar09, we really appreciate it! 💛  


Wusstest du, dass es auch eine DACH-Community gibt?
Nimm an regionalen Unterhaltungen teil, indem du deine Spracheinstellungen änderst


Did you know that the Community is available in other languages?
Join regional conversations by
changing your language settings

0 Upvotes
GreyHamilton
Solution
Participant | Elite Partner
Participant | Elite Partner

Calculate difference between start date and end date in a custom object

SOLVE

@Shuler Love this solution. I believe yours is the ideal solution, but for anyone who isn't comfortable with APIs or custom code I figured out a way to do it with a 'Days Between' calculated field and the Format feature in a workflow.

Here is the workflow utilizing the Format function. I tagged this as "Original Duration" as you could have it not re-enroll so it's only set once. Depending on if your dates change, you could potentially benefit from a "snapshot" of the original duration. The round was added because it was adding a 0 decimal point even though the property limited them.

GreyHamilton_0-1680554288647.png

 

Results: 

GreyHamilton_1-1680554634513.png

 

Did this post help solve your problem? If so, please mark it as a solution.

Grey Hamilton, Sr. Technical Consultant
Bayard Bradford

Advanced CRM Implementation | HubSpot Custom Integration Services | Datawarehouse.io Apps for HubSpot
Elite HubSpot Solutions Partner
> Follow me on LinkedIn
ankitparmar09
Top Contributor

Calculate difference between start date and end date in a custom object

SOLVE

Hello @Shuler 

Thanks for reaching out. You can use the code, it can help you.

 

{# Date Format Code #}
{% set date = event.event_end_date %}
{% set dateArr = date|split("/", 1000) %}
{% set useDate = [
(dateArr[2]|int + 2000),
(dateArr[0]|length == 1 ? '0' + dateArr[0] : dateArr[0]),
(dateArr[1]|length == 1 ? '0' + dateArr[1] : dateArr[1])
]
%}

{% set enddate = (useDate[0] + "-" + useDate[1] + "-" + useDate[2] + "T14:31:30+0530")|strtotime("yyyy-MM-dd'T'HH:mm:ssZ")|unixtimestamp %}

{% set today = (local_dt|unixtimestamp) %}

{% if (enddate <= today ) %}

// Code here


{% endif %}

 

MiaSrebrnjak
Community Manager
Community Manager

Calculate difference between start date and end date in a custom object

SOLVE

Hi @Shuler

 

Thank you for reaching out to the Community! 

I wanted to tag in a couple of subject matter experts to see if they have any input on this matter:

Hi @deepikaverma@GreyHamilton@GCasali, do you have any advice for @Shuler? Thank you!

 

Cheers
Mia, Community Team 


Wusstest du, dass es auch eine DACH-Community gibt?
Nimm an regionalen Unterhaltungen teil, indem du deine Spracheinstellungen änderst


Did you know that the Community is available in other languages?
Join regional conversations by
changing your language settings