CMS Development

ben-duchy
Top Contributor

Difference between dates

SOLVE

Does anyone know if it's possible to display the difference between 2 sets of dates within HubDB?

 

So for example if I create 2 date columns, is there any HubL math operators that can calculate how many days difference there are?

0 Upvotes
1 Accepted solution
piersg
Solution
Key Advisor

Difference between dates

SOLVE

You just need this to get difference in days really:

 

{% set end_time = unixtimestamp(row.end_datetime) %}
{% set start_time = unixtimestamp(row.start_datetime) %}
{% set days = (end_time - start_time)//24//60//60//1000 %}

 

 

View solution in original post

0 Upvotes
8 Replies 8
Thrive
Contributor | Elite Partner
Contributor | Elite Partner

Difference between dates

SOLVE

Hi there, we have built an app for the marketplace that allows you to automatically calculate the time between 2 dates. Feel free to check it out here: https://ecosystem.hubspot.com/marketplace/apps/marketing/data-management/calculations-3449553

0 Upvotes
piersg
Key Advisor

Difference between dates

SOLVE

No worries, glad I could help 🙂

0 Upvotes
piersg
Solution
Key Advisor

Difference between dates

SOLVE

You just need this to get difference in days really:

 

{% set end_time = unixtimestamp(row.end_datetime) %}
{% set start_time = unixtimestamp(row.start_datetime) %}
{% set days = (end_time - start_time)//24//60//60//1000 %}

 

 

0 Upvotes
ben-duchy
Top Contributor

Difference between dates

SOLVE

@piersgyou're a genius! Thank you so much! 

0 Upvotes
piersg
Key Advisor

Difference between dates

SOLVE

Hi Ben,

 

I did this to get the length of an event (i.e. difference between two dates):

 

{% if row.end_datetime %}
  {% set end_time = unixtimestamp(row.end_datetime) %}
  {% set start_time = unixtimestamp(row.start_datetime) %}
  {# get length in hours #}
  {% set length = (end_time - start_time)//60//60//1000 %}
  {% if length > 24 %}
    {% set length = length/24 %}
    {% set days = length|round(0, 'floor') %}
    {% set length = days~' days' %}
  {% elif length > 12 %}
    {% set length = 'All day event' %}
  {% elif length == 1 %}
    {% set length = length~' hour' %}
  {% elif length < 1 %}
    {% set length = (end_time - start_time)//60//1000 %}
    {% set length = length~' minutes' %}
  {% else %}
    {% set length = length~' hours' %}
  {% endif %}
{% endif %}

 

ben-duchy
Top Contributor

Difference between dates

SOLVE

Hi @piersg, thanks I'll give it a go

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Difference between dates

SOLVE

Hiya @ben-duchy !

Does it have to be hubl?  Will a js solution work

0 Upvotes
ben-duchy
Top Contributor

Difference between dates

SOLVE

Hi @dennisedson,

I don't think JS would work in this case as the data is being stored in HubDB and I'm not sure if you can pull data in from this using JS. Also I'm more comfortable with HubL over JS.

 

Thanks anyway

0 Upvotes