---
title: Compare two dates using HubL
description: Calculate the difference between two HubL dates in HubSpot in order to do things like display past and upcoming events from a HubDB table.
---

[Skip to Content](https://www.stephanieogaygarcia.com/hubspot-website-development/calculate-the-difference-between-two-dates-using-hubl#main-content)

[![Stephanie O'Gay Garcia](https://www.stephanieogaygarcia.com/hubfs/Logos/stephanieogaygarcia_logo.svg "Stephanie O'Gay Garcia")](https://www.stephanieogaygarcia.com/)

Open Navigation

- [About](https://www.stephanieogaygarcia.com/about)
- [Services](https://www.stephanieogaygarcia.com/services)
- [Products](https://www.stephanieogaygarcia.com/shop)
- [Work](https://www.stephanieogaygarcia.com/portfolio-work)
- [Blog](https://www.stephanieogaygarcia.com/hubspot-website-development)
- [Contact](https://www.stephanieogaygarcia.com/contact)

- [About](https://www.stephanieogaygarcia.com/about)
- [Services](https://www.stephanieogaygarcia.com/services)
- [Products](https://www.stephanieogaygarcia.com/shop)
- [Work](https://www.stephanieogaygarcia.com/portfolio-work)
- [Blog](https://www.stephanieogaygarcia.com/hubspot-website-development)
- [Contact](https://www.stephanieogaygarcia.com/contact)

[HubSpot Sprocket Icon  HubSpot Website Development ](https://www.stephanieogaygarcia.com/hubspot-website-development)

[hubl](https://www.stephanieogaygarcia.com/hubspot-website-development/tag/hubl)

# Compare two dates using HubL

 Technical Difficulty: Advanced

 Written by [ Stephanie O'Gay Garcia ](https://www.stephanieogaygarcia.com/hubspot-website-development/author/stephanie-ogay-garcia)

 First Published:  October 20, 2023

 Last Updated:  June 11, 2025

⚠️ Note that this post hasn't been updated for at least a year and the information may be outdated, proceed with caution! *(Last updated: June 11, 2025)*

There may be times in which you want to calculate the difference between two date fields in HubSpot, such as:

- Checking if one date happens before or after another
- Displaying past vs upcoming events (using HubDB, or a custom field on blog posts)
- Calculating the duration of an event

## Ensure dates are in unix formatting

Before comparing the two dates, you'll want to make sure that both are in unix format, which may already be the case depending on where you're pulling the date from. Below are some examples of dates you might be using and their default formats:

- [**Page or Blog created/published/updated dates**](https://developers.hubspot.com/docs/cms/hubl/variables#general-variables). The `content.publish_date` `content.created` and `content.updated` variables are in UTC time and will need to be converted to unix timestamps.
- **HubDB date/date and time columns**. When calling a column of either of these types, the date is already output as a unix timestamp.
- **Module date/date and time fields**. When using a field of these types from a custom module, the date is output in UTC time and will need to be converted to a unix timestamp.

To convert a date to its unix timestamp in HubL, you can either use the [`unixtimestamp`](https://developers.hubspot.com/docs/cms/hubl/functions#unixtimestamp) function or the [`unixtimestamp`](https://developers.hubspot.com/docs/cms/hubl/filters#unixtimestamp) filter. To convert a blog post's publish date to unix formatting, the function will look like:

`unixtimestamp(content.publish_date)`

and the filter will look like:

`content.publish_date|unixtimestamp`

## Use a calculation to compare the two dates

Once you've ensured that your dates are in unix format, you can calculate the difference between them. 

For example, in a module with two date fields "Start Date" (variable name `start_date`) and "End Date" (variable name `end_date`), your calculation might look like this:

```
{% set start_date = module.start_date|unixtimestamp %}
{% set end_date = module.end_date|unixtimestamp %}

{% set difference = end_date - start_date %}
```

You can use then use the "difference" variable as needed. 

## Examples and Use Cases

### Add a warning for outdated content

In my blog, you'll notice a warning at the top of posts that haven't been updated for over a year. This is done by calculating the difference between the current date using the [`local_dt`](https://developers.hubspot.com/docs/cms/hubl/variables#general-variables) variable and the post's last updated date, like this:

```
{% set lastupdateddt = unixtimestamp(content.updated) %}
{% set currentdt = unixtimestamp(local_dt) %}

{% set datecalc = currentdt - lastupdateddt %}

{% if datecalc > 31556926000 %}
  <div class="blog-post__warning">⚠️ Note that this post hasn't been updated for at least a year and the information may be outdated, proceed with caution! <i>(Last updated: {{ content.updated|datetimeformat("%B %e, %Y") }})</i></div>
{% endif %}
```

The number 31556926000 is the amount of miliseconds there are in a year.

Note: In the example above I simply compared the number of miliseconds in a year, but HubSpot also offers [`minus_time`](https://developers.hubspot.com/docs/cms/hubl/filters#minus-time) and [`plus_time`](https://developers.hubspot.com/docs/cms/hubl/filters#plus-time) filters to add and subtract an amount of time to a datetime object. 

### Distinguish between upcoming and past events from a HubDB table

You may have a HubDB table for your events and want to show upcoming events in one section and past events in another.

```
{# Upcoming Events #}
{% set upcoming_events_queryparam = "&start_date__gt=" + unixtimestamp(local_dt) %}
{% set upcoming_events = hubdb_table_rows(hubdb_table_id, upcoming_events_queryparam) %}

<h2>Upcoming Events</h2>
<ul>
  {% for row in upcoming_events %}
    <li>{{ row.name }}</li>
  {% endfor %}
</ul>

{# Past Events #}
{% set past_events_queryparam = "&start_date__lte=" + unixtimestamp(local_dt) %}
{% set past_events = hubdb_table_rows(hubdb_table_id, past_events_queryparam) %}

<h2>Past Events</h2>
<ul>
  {% for row in past_events %}
    <li>{{ row.name }}</li>
  {% endfor %}
</ul>
```

In the example above, we have a HubDB table that has a "Start Date" date type column (variable name `start_date`). 

To pull upcoming events, we create a variable `upcoming_events_queryparam` where we get the current datetime using the `local_dt` variable (and format it using the `unixtimestamp` function). Then we use the greater than (`gt`) operator to compare the two. We get the HubDB rows for upcoming events using the [`hubdb_table_rows`](https://developers.hubspot.com/docs/cms/hubl/functions#hubdb-table-rows) function and our query.

Past events are pulled in the same way but using the less than or equal to (`lte`) operator.

### Calculate the duration of an event

Another reason we may want to find the difference between two dates is to calculate the duration of an event. 

To do this, we'll take our simple calculation from above where we get the difference between the Start Date and the End Date fields in a module. The "duration" output here will be a unix timestamp with the number of miliseconds. 

To convert that timestamp to hours, for example, we could use a calculation (there are 3,600,000 miliseconds in an hour):

```
{% set start_date = module.start_date|unixtimestamp %}
{% set end_date = module.end_date|unixtimestamp %}

{% set difference = end_date - start_date %}

{% set difference_in_hours = difference / 3600000 %}
```

 

If you found this helpful, buy me a coffee! ❤️

←  Previous Article

[Timezones on HubSpot's "Date" and "Date and Time" module fields](https://www.stephanieogaygarcia.com/hubspot-website-development/timezones-on-hubspots-date-and-date-and-time-module-fields)

Next Article  →

[Customize the embedded video in a HubSpot Video module](https://www.stephanieogaygarcia.com/hubspot-website-development/customize-the-embedded-video-in-a-hubspot-video-module)

Contact

## Get help from a HubSpot CMS Expert

- Custom HubSpot themes and reusable and easy-to-use HubSpot templates and modules
- Technical support and guidance on the HubSpot CMS

[ Tell me about your project ](https://www.stephanieogaygarcia.com/project-details)

##### Contact

Need help with a HubSpot CMS project? Get in touch!

[Email](mailto:stephanie@stephanieogaygarcia.com) | [Contact](https://www.stephanieogaygarcia.com/contact)

##### About

- [ About ](https://www.stephanieogaygarcia.com/about)
- [ Portfolio ](https://www.stephanieogaygarcia.com/portfolio-work)
- [ LinkedIn ](https://www.linkedin.com/in/stephanieogaygarcia/)
- [ Facebook ](https://www.facebook.com/stephanieogaygarcia)
- [ Instagram ](https://www.instagram.com/stephanieogaygarcia/)

##### Services

- [ HubSpot Website Development ](https://www.stephanieogaygarcia.com/services/hubspot-website-development)
- [ HubSpot CMS Technical Support ](https://www.stephanieogaygarcia.com/services/hubspot-cms-technical-support)
- [ Custom HubSpot CMS Themes ](https://www.stephanieogaygarcia.com/custom-hubspot-cms-hub-themes)
- [ Pricing ](https://www.stephanieogaygarcia.com/services#pricing)

##### Latest Articles

- [How to add an llms.txt page to your HubSpot website](https://www.stephanieogaygarcia.com/hubspot-website-development/add-an-llmstxt-page-to-your-hubspot-website)
- [Switch an existing HubSpot custom module from global to local](https://www.stephanieogaygarcia.com/hubspot-website-development/switch-an-existing-hubspot-custom-module-from-global-to-local)
- [Overriding HubSpot's new forms styling from theme settings](https://www.stephanieogaygarcia.com/hubspot-website-development/overriding-hubspots-new-forms-styling-from-theme-settings)
- [All Articles](https://www.stephanieogaygarcia.com/hubspot-website-development)

---

 Copyright © 2018 - 2026 Stephanie O'Gay Garcia. All Rights Reserved.   
Freelance HubSpot CMS Developer based in Toronto, Canada 🇨🇦   
[Terms of Service](https://www.stephanieogaygarcia.com/terms-of-service) | [Website Terms of Use](https://www.stephanieogaygarcia.com/website-terms-of-use) | [Privacy](https://www.stephanieogaygarcia.com/privacy-policy) | [Cookies](https://www.stephanieogaygarcia.com/cookie-policy) | [LLMs](https://www.stephanieogaygarcia.com/llms.txt)

Scroll to Top

```json
{
  "@context" : "https://schema.org",
  "@id" : "https://www.stephanieogaygarcia.com/#person",
  "@type" : "Person",
  "description" : "Freelance HubSpot CMS Developer specializing in custom HubSpot website development, themes, templates, and bespoke modules for small–mid sized B2B and SaaS companies.",
  "image" : "https://www.stephanieogaygarcia.com/hubfs/Logos/stephanieogaygarcia_logo.svg",
  "jobTitle" : "Freelance HubSpot CMS Developer",
  "knowsAbout" : [ "HubSpot CMS", "HubSpot themes", "HubSpot templates", "HubSpot modules", "HubDB", "Website migrations to HubSpot CMS", "Figma to HubSpot implementation" ],
  "name" : "Stephanie O'Gay Garcia",
  "sameAs" : [ "https://www.linkedin.com/in/stephanieogaygarcia/", "https://www.facebook.com/stephanieogaygarcia", "https://www.instagram.com/stephanieogaygarcia/", "https://ecosystem.hubspot.com/marketplace/solutions/stephanie-ogay-garcia/" ],
  "url" : "https://www.stephanieogaygarcia.com/",
  "worksFor" : {
    "@id" : "https://www.stephanieogaygarcia.com/#organization"
  }
}
```

```json
{
  "@context" : "https://schema.org",
  "@id" : "https://www.stephanieogaygarcia.com/#organization",
  "@type" : "Organization",
  "address" : {
    "@type" : "PostalAddress",
    "addressCountry" : "IE",
    "addressLocality" : "Dublin",
    "postalCode" : "A94"
  },
  "contactPoint" : {
    "@type" : "ContactPoint",
    "areaServed" : "Worldwide",
    "availableLanguage" : [ "en", "es" ],
    "contactType" : "sales",
    "email" : "stephanie@stephanieogaygarcia.com"
  },
  "description" : "Portfolio and business site of freelance HubSpot CMS Developer Stephanie O'Gay Garcia, based in Dublin, Ireland.",
  "logo" : "https://www.stephanieogaygarcia.com/hubfs/Logos/stephanieogaygarcia_logo.svg",
  "name" : "Stephanie O'Gay Garcia",
  "sameAs" : [ "https://www.linkedin.com/in/stephanieogaygarcia/", "https://www.facebook.com/stephanieogaygarcia", "https://www.instagram.com/stephanieogaygarcia/", "https://ecosystem.hubspot.com/marketplace/solutions/stephanie-ogay-garcia/" ],
  "url" : "https://www.stephanieogaygarcia.com/"
}
```

```json
{
  "@context" : "https://schema.org",
  "@id" : "https://www.stephanieogaygarcia.com/#website",
  "@type" : "WebSite",
  "name" : "Stephanie O’Gay Garcia – Freelance HubSpot CMS Developer",
  "publisher" : {
    "@id" : "https://www.stephanieogaygarcia.com/#organization"
  },
  "url" : "https://www.stephanieogaygarcia.com/"
}
```

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Stephanie O'Gay Garcia",
    "url" : "https://www.stephanieogaygarcia.com/hubspot-website-development/author/stephanie-ogay-garcia"
  },
  "dateModified" : "2025-06-11T15:16:11.355Z",
  "datePublished" : "2023-10-20T13:50:05.000Z",
  "headline" : "Compare two dates using HubL",
  "mainEntityOfPage" : {
    "@id" : "https://www.stephanieogaygarcia.com/hubspot-website-development/calculate-the-difference-between-two-dates-using-hubl",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.stephanieogaygarcia.com/hubfs/Logos/stephanieogaygarcia_logo.svg"
    }
  }
}
```