Reputation: 77
Previously I had some code to display a banner on certain date ranges for example Valentines Day banner to show from Feb 1st to 14th. This is currently what I have and works like a charm.
{% assign start1 = "2022-04-24" | date: '%s' %}
{% assign end1 = "2022-05-09" | date: '%s' %}
{% assign today = "now" | date: '%s' %}
{% if start1 <= today and today <= end1 %}
<div>Banner</div>
{% endif %}
I want to know if there is a way to remove the year setting in the code and have it work the same way so I don't need to go into the code every year and update it with the current year number. Just need it to work off of the month and day.
Upvotes: 0
Views: 423
Reputation: 4930
You can get the current year from now and create your date string dynamically. In the below code, year variable will get the current year and then add it to the beginning of your dates before converting them to timestamp.
{% assign year = "now" | date: "%Y" %}
{% assign start1 = "-04-24" | prepend: year | date: '%s' %}
{% assign end1 = "-05-09"| prepend: year | date: '%s' %}
{% assign today = "now" | date: '%s' %}
{% if start1 <= today and today <= end1 %}
<div>Banner</div>
{% endif %}
Upvotes: 1