If logic for Dates

Shopify Partner
23 0 0

Hey there -

I'm trying to write some simple code regarding dates, but cannot figure it out. This is for email notifications.

Rule: If today is greater than the 18th, than i'd like to show today's date (Month / Day). If it isn't I'd like it to show today's month and the current date.

Here's what I have so far:

{% if {{ 'now' | date: "%d" }} > '18' %}
<h3>{{ 'now' | date: "%b" }}. {{ 'now' | date: "%d" }}</h3>
{% else %}
<h3>{{ 'now' | date: "%b" }}. 18</h3>
{% endif %}

 

Thanks for any help in advance.

 

 

0 Likes
Highlighted
Astronaut
1925 0 362

Try this:

{% assign date_month = 'now' | date: '%b' %}
{% assign date_day = 'now' | date: '%d' | abs %}
{% if date_day > 18 %}
  {% assign date_day = 'now' | date: '%d' %}
{% else %}
  {% assign date_day = '18' %}
{% endif %}

<h3>{{ date_month }}. {{ date_day }}</h3>

 

0 Likes
Shopify Expert
9584 34 1325

^ Do keep in mind that 'now' is very unlikely to be the current date/time. It will be whatever the time of last server cache was.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Partner
23 0 0

This works great. Thank you, Alex!

0 Likes
Shopify Partner
23 0 0

Jason -

How often does the server cache refresh?

0 Likes
Shopify Expert
9584 34 1325

There's not a set time as it will be refreshed as it needs to be (eg: when content is changed). Best to assume that the difference may be hours. If that's a problem, use JavaScript instead. I've got a blog post on my site that has a demo of this. Here's a screenshot I took of the page a while ago:

^ Note the difference in time.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Partner
23 0 0

Thanks for the heads up. Should be good with that margin of error.

0 Likes
New Member
4 0 0

Thanks all - found these posts helpful for my situation, and have built something related and sharing in case someone else finds it helpful.

 

In our situation, each product has an available date. I wanted to be able to add a preorder notice to tell consumers that if they placed an order it would be shipped after X date.

 

To do this, I used the custom fields app and set up a comparison on year, month and date thus:

 

<!--Sets current date as three separate fields-->
{% assign currentdateyear = 'now' | date: "%Y" %}
{% assign currentdatemonth = 'now' | date: "%m" %}
{% assign currentdateday = 'now' | date: "%e" %}
<!--Sets pub date as three separate fields-->
{% assign pubdateyear = product.metafields.custom_fields["published_date_datefield"] | date: "%Y" %}
{% assign pubdatemonth = product.metafields.custom_fields["published_date_datefield"] | date: "%m" %}
{% assign pubdateday = product.metafields.custom_fields["published_date_datefield"] | date: "%e" %}

<!--Find if product is published in the next year. If it is, include the pre-order notice snippet -->
{% if currentdateyear < pubdateyear %}
    {% include 'pre-order-notice-content' %}
{% endif %}
<!--Find if product is published in the same year, but an earlier month-->
{% if currentdateyear == pubdateyear %}
    {% if currentdatemonth < pubdatemonth %}
        {% include 'pre-order-notice-content' %}
    {% endif %}
{% endif %}
<!--Find if product is published in the same year and same month but earlier day-->
{% if currentdateyear == pubdateyear %}
    {% if currentdatemonth == pubdatemonth %}
        {% if currentdateday < pubdateday %}
        {% include 'pre-order-notice-content' %}
        {% endif %}    
    {% endif %}
{% endif %}

 

 

0 Likes