---
title: "How to HubL: Add Dynamic Dates and Times to Automated Emails"
description: Don't stress over changing dates in your content before your automated HubSpot email sends, by learning how to add dynamic dates with HubL.
---

[HubSpot CMS, Email, and Front-End Dev Insights | Alyssa Wilie](https://alyssawilie.com/blog)

# [How to HubL: Add Dynamic Dates and Times to Automated Emails](https://alyssawilie.com/blog/add-dynamic-dates-to-automated-emails)

 Written by [Alyssa Wilie](https://alyssawilie.com/blog/author/alyssa-wilie) | May 22, 2023

Showing the current day's date, providing coupon expiration information, announcing the end of a sale; there's a plethora of reasons to add dates to an email. However it can be tedious to update one such line for every email send and can be even more difficult to maintain when doing so in an automated email. To solve for this we can use HubL to write small snippets of code to create *dynamic *dates. A dynamic date will automatically update depending on the day the email is sent and/or based on other defined criteria. So let's get started:

## local\_dt and format\_datetime

For dynamic dates there is a specific variable and function that you will need for just about any snippet you'll write. The variable local\_dt outputs a datetime object of the current time in the time zone defined in your Account Defaults Settings. format\_datetime (formally known as datetimeformat) is a function that will let us format that datetime object into something more readable. You can also use format\_date or format\_time if you're only interested in displaying one or the other.

The format functions have two required parameters—the datetime object you want to format and, of course, the format. There are four predefined formats you can use (short, medium, long, full) but you can also provide any custom format using [Unicode Locale Data Markup Language](https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Pattern_Examples). To show how the basics of this function works we'll start by outputting the simplest of dates, today's date, using the four predefined formats.

It's important to note that, by default, format\_datetime outputs the time in UTC (Coordinated Universal Time). You can use the two optional parameters, time zone and locale, to localize your date/time output.

Time zone allows you to dictate the time zone of the outputted time by providing a name from the IANA (Internet Assigned Numbers Authority) [timezone database](https://data.iana.org/time-zones/tzdb/). You can also use the local\_time\_zone variable to use your HubSpot accounts time zone setting. The example below shows an output using my local\_time\_zone (America/Chicago) and Asia/Tokyo.

Locale sets a specific [locale](https://www.localeplanet.com/icu/) to use. In the following example we'll change the locale to 'es' to change the language to Spanish.

## plus\_time and minus\_time

Next let's dive into showing dates in the future and in the past. To do this we can use either of the two *filters, *plus\_time and minus\_time. Each of these filters has only two parameters—the amount you want to add/subtract and the unit you want to add/subtract by. There are [15 different units](https://developers.hubspot.com/docs/cms/hubl/filters#minus-time), but in email you're most likely to use days, weeks, months, and years. Below are a few examples using these filters. Note that the filter should be used on the time variable and not the whole function.

## Advanced Dates

We've learned the basic variables, functions, and filters for writing dynamic dates so next is learning to use these in more advanced use cases. For instance, what if we only want to show *business days* and skip over weekends when outputting a date in the future? Or maybe instead of exactly two weeks ahead, we want to output two*Thursdays *ahead.

### Business Days

To only output business days we'll start with creating a couple of variables: day\_increase will default to how many days we originally want to go into the future by and date\_weekday will get the weekday that the original increase falls on. We can use the EEEE unicode format to get the weekday.

We'll then run an if else statement to check if date\_weekday falls on a Saturday or Sunday. If it falls on a Saturday, we'll add 2 to our day\_increase variable. If it falls on a Sunday we'll only add 1 to our day\_increase variable.

Then we only need to output our date using day\_increase as our amount variable for plus\_time.

### Weekday Specific

To get a specific weekday will involve just a little more footwork. We'll start it off by setting a few key variables: date will be our original future date, weekday will be the weekday that date lands on, and accepted\_weekday will be the weekday we *require *our future date to land on.

Similar to looking for business days we'll then set up an in if else statement. *If *weekday is the same as accepted\_weekday we'll go ahead and output our formatted date variable.

If weekday isn't the weekday we want though we'll then move into our *else *statement where we'll run through a for loop. Since are accepted weekday will be within the next 6 days we'll run the for loop through a range of 1 to 6 to find the nearest day from our date that matches our accepted\_weekday. In the for loop we'll create a next\_weekday variable to set the future weekday using x from our loop. Then we'll check if next\_weekday matches our accepted\_weekday and, if it does, we'll output that day as our formatted date.

And so the above will only ever show a Thursday date!

## Add Your Dynamic Date to Your Email

Now at the end of all this you've learned you may be asking "now where do I *put* this code?" You have a couple of options actually. If you're just outputting a basic date with minimal code you can drop it right into the text of your email. It'll show up as code in the email editor, but in the email preview and sent emails it'll show the rendered date.

For more complex code, like mentioned in the advanced section, it may be more intuitive to [create a custom module](https://knowledge.hubspot.com/design-manager/create-and-edit-modules) in the Design Manager. You can create a module specifically just to house your dynamic date code or code out the entire section you're using the date in. Also, if you're building a custom coded template, instead of using HubSpot's drag-and-drop email, you can place the code directly in the template. Putting your code in a module or template will render it in the email editor exactly as it'll appear in the email.

### Formatting Personalization Tokens

In some use cases you may want to output a date from a contact's data in HubSpot. However, it's not possible to use filters and functions on personalization tokens in *basic *emails. You would need to have a subscription tier that includes the [programmable emails](https://knowledge.hubspot.com/email/create-programmable-emails) beta to be able to mutate personalization tokens. If you do have the programmable emails beta—you won't be able to mutate tokens directly in the email, but you can make email modules with this capability by flipping on the programmable email switch.

 

With the help of dynamic dates you can now more easily set and forget your automated emails or even save time when manually sending emails from a template with predefined content. 

[View full post](https://alyssawilie.com/blog/add-dynamic-dates-to-automated-emails)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Alyssa Wilie"
  },
  "dateModified" : "2026-01-29T19:35:59.653Z",
  "datePublished" : "2023-05-22T14:00:00Z",
  "headline" : "How to HubL: Add Dynamic Dates and Times to Automated Emails",
  "image" : {
    "@type" : "ImageObject",
    "height" : 627,
    "url" : "https://5019295.fs1.hubspotusercontent-na1.net/hubfs/5019295/add-dynamic-dates.png",
    "width" : 1200
  },
  "mainEntityOfPage" : "https://alyssawilie.com/blog/add-dynamic-dates-to-automated-emails",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "Web Developer Blog"
  }
}
```