CMS Development

IsabellaPreda
Member

Add Token to Show the Current Month in Email

SOLVE

Hi Hubspot Forum,

 

I would like to add a token to an email that shows the end of the current month, for example: "please submit all documentation no later than three days prior to [December 31st]". This would be automated even when the month changes. Is there a way to do this with code? 

 

Any help is appreciated!

1 Accepted solution
dennisedson
Solution
HubSpot Product Team
HubSpot Product Team

Add Token to Show the Current Month in Email

SOLVE

Ah, apologies.  I was under the impression that you only wanted the date in that font and color

Try this

{% set currentMonth = local_dt|datetimeformat('%B') %} 
{% set Months31 = ["January", "October", "December", "March", "May", "July", "August", "October" ]%} 
{% set Months30 = ["April", "June", "September", "November" ]%}  
{% set Months2829 = ["February"] %}
{% set leapyear = year is divisibleby 100 ? year is divisibleby 400 : year is divisibleby 4 %}

<span style = "color:#3b3b3b; font-family: lato; font-size: 14px">
{{ module.text_field }}


{% if Months31 is containing currentMonth %} 
{{ currentMonth }} 31, {{ year }} 
{% elif Months30 is containing currentMonth %} {{ currentMonth }} 30, {{ year }}
{% elif Months2829 is containing currentMonth %} {{ currentMonth }} {% if leapyear is false %}28 {% elif leapyear is true %}29{% endif %}, {{ year }} {% endif %}</span>

You will notice that I moved the beginning span tag to above the text field

View solution in original post

0 Upvotes
8 Replies 8
EBurton2
Member

Add Token to Show the Current Month in Email

SOLVE

@dennisedson Could you modify this to work for my use case? I need to send an email at end of a month which contains the name of the following month. For example, if I send an email in April, I want the module to contain May, so on and so fouth. Something like this in Javascript&colon; https://stackoverflow.com/questions/499838/javascript-date-next-month

0 Upvotes
IsabellaPreda
Member

Add Token to Show the Current Month in Email

SOLVE

Got it, thank you! I can see the text and have it all on one line now. Do you know how to make the font type/colour match the 'December 31st, 2021?' 

 

It's almost completed. 🙂 

 

text.PNG

0 Upvotes
dennisedson
Solution
HubSpot Product Team
HubSpot Product Team

Add Token to Show the Current Month in Email

SOLVE

Ah, apologies.  I was under the impression that you only wanted the date in that font and color

Try this

{% set currentMonth = local_dt|datetimeformat('%B') %} 
{% set Months31 = ["January", "October", "December", "March", "May", "July", "August", "October" ]%} 
{% set Months30 = ["April", "June", "September", "November" ]%}  
{% set Months2829 = ["February"] %}
{% set leapyear = year is divisibleby 100 ? year is divisibleby 400 : year is divisibleby 4 %}

<span style = "color:#3b3b3b; font-family: lato; font-size: 14px">
{{ module.text_field }}


{% if Months31 is containing currentMonth %} 
{{ currentMonth }} 31, {{ year }} 
{% elif Months30 is containing currentMonth %} {{ currentMonth }} 30, {{ year }}
{% elif Months2829 is containing currentMonth %} {{ currentMonth }} {% if leapyear is false %}28 {% elif leapyear is true %}29{% endif %}, {{ year }} {% endif %}</span>

You will notice that I moved the beginning span tag to above the text field

0 Upvotes
IsabellaPreda
Member

Add Token to Show the Current Month in Email

SOLVE

Hi Dennis, thank you for the screenshots. I was able to create a custom module with the field text, however, when I try adding the copy I'd like next to the date on the email editor page, it doesn't show up in the email. Do I have to add the copy to the design manager screen instead? I've tried that approach and the text still doesn't show up. Any info is appreciated.

 

Thank you!

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Add Token to Show the Current Month in Email

SOLVE

@IsabellaPreda 

Make sure you are using a text field and not a rich text field 😉

And make sure that you keep it the same name as I have in my example above.  (which is just the default text)  If  you want to change the name of the field, you will have to make sure to update the code.

0 Upvotes
IsabellaPreda
Member

Add Token to Show the Current Month in Email

SOLVE

Thank you Dennis, this is very helpful! If I wanted to add code for the month of February since it has 28 days, how would that look like? 

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Add Token to Show the Current Month in Email

SOLVE

@IsabellaPreda 

Looks like you were contacted support as well (they reached out to me)

I updated the code to account for February as well as leap years ( thanks @Teun  for the assist there! )

Also added the color and font that you wanted to the date.  I would suggest making sure you have a backup font in case lato fails to load. 

 

You also mentioned in the support request that you wanted it to appear on the same line as the text preceeding it.  I would suggest creating a custom module for this and creating a text field for the copy. 

Here is the code from the module I made as well as a screenshot showing the field I added for the copy

{% set currentMonth = local_dt|datetimeformat('%B') %} 
{% set Months31 = ["January", "October", "December", "March", "May", "July", "August", "October" ]%} 
{% set Months30 = ["April", "June", "September", "November" ]%}  
{% set Months2829 = ["February"] %}
{% set leapyear = year is divisibleby 100 ? year is divisibleby 400 : year is divisibleby 4 %}


{{ module.text_field }}

<span style = "color:#3b3b3b; font-family: lato; font-size: 14px">
{% if Months31 is containing currentMonth %} 
{{ currentMonth }} 31, {{ year }} 
{% elif Months30 is containing currentMonth %} {{ currentMonth }} 30, {{ year }}
{% elif Months2829 is containing currentMonth %} {{ currentMonth }} {% if leapyear is false %}28 {% elif leapyear is true %}29{% endif %}, {{ year }} {% endif %}</span>

 

 

Cursor_and_Design_Manager___HubSpot.png

dennisedson
HubSpot Product Team
HubSpot Product Team

Add Token to Show the Current Month in Email

SOLVE

Hey @IsabellaPreda !

You can get the current month using the local date time variable.  Then, you can apply date time format filter to print in the format that you want ( eg December versus 12 )

You can also get the current year if that is of use year variable.

What you cannot get for free is the end of each month day.  For this I would set some variables that contain an array of months matching the same quantity of days; then, write an if statement to print for each.

Here is an example of what I am talking about.  Note that I have not added all months.  Just some to show you the concept:

{% set currentMonth = local_dt|datetimeformat('%B') %}
{% set Months31 = ["January", "October", "December" ]%}
{% set Months30 = ["April", "June", "September", "November" ]%}

{% if  Months31 is containing currentMonth %}
{{ currentMonth }}, 31 {{ year }}
{% elif Months30 is containing currentMonth %}
{{ currentMonth }}, 30 {{ year }}
{% endif %}

Hope this helps!