If logic with dates to show based on day

Solved
Innershop
Tourist
10 0 0

I am trying to set up a logic that would work in such a way that if a day that is set up in a custom field is between 1-10, it would show "Early" + month, 11-19 would show "Mid" + Month and 20-31 would show "Late" + Month.

Any directions on how to write this would help A LOT! 

Many Thanks!!

0 Likes
Innershop
Tourist
10 0 0

Have so far written this but unsure why it always defaults to the first rule regardless on the date number..

 

{% if collection.metafields.teams.ships | date: "%d" < 9 %}
<div class="sub-title text-white space-80 bold">Estimated shipping: Early {{ collection.metafields.teams.ships | date: "%B" }}.</div>
{% elsif collection.metafields.teams.ships | date: "%d" >= 10 and < 20 %}
<div class="sub-title text-white space-80 bold">Estimated shipping: Mid {{ collection.metafields.teams.ships | date: "%B" }}.</div>
{% else %}
<div class="sub-title text-white space-80 bold">Estimated shipping: Late {{ collection.metafields.teams.ships | date: "%B" }}.</div>
{% endif %}

0 Likes
bdowling
Pathfinder
80 13 41

This is an accepted solution.

Hi, what app is the "collection.metafields.teams.ships" data coming from? You should make sure the "collection.metafields.teams.ships" is formatted as expected.

If it is then try this instead of what you have:

 

 

{% assign ship_day =  collection.metafields.teams.ships | date: "%d" | abs %}
{% if ship_day < 9 %}
<div class="sub-title text-white space-80 bold">Estimated shipping: Early {{ collection.metafields.teams.ships | date: "%B" }}.</div>
{% elsif ship_day >= 10 and < 20 %}
<div class="sub-title text-white space-80 bold">Estimated shipping: Mid {{ collection.metafields.teams.ships | date: "%B" }}.</div>
{% else %}
<div class="sub-title text-white space-80 bold">Estimated shipping: Late {{ collection.metafields.teams.ships | date: "%B" }}.</div>
{% endif %}

 

 

Innershop
Tourist
10 0 0

You superstar! That worked like a charm. Just out of curiosity - what was wrong with the logic I was proposing?

0 Likes
bdowling
Pathfinder
80 13 41

The logic was fine. This code may have worked if it wasn’t for quirks with Liquid.

I can’t tell for sure without trying out the code with the app that’s being used but it came down to 2 possible points of Liquid being tricky.

A: Liquid couldn’t compare a month day to your numbers so adding “| abs” made sure it became a number.

B: There also may have been syntax confusion from Liquid in the comparison statement so assigning it to a variable in another line eliminates that (also makes it easier to read)

Innershop
Tourist
10 0 0

Thanks for your generosity! Just learned something.

Have a great one!

0 Likes