Trouble with Date Operator IF statements

Justin29
New Member
6 0 0

Hi all,

I'm trying to automate displaying an image depending on the date/time. Here's te code I'm trying to make work.

{%if 'now' | time_tag: '%Y-%m-%d:%H:%M:%S' <= "2016-12-04:12:00:00" %}<img src="{{ '16_holiday-sd-1.png' | asset_url }}"/>{% else %}<img src="{{ '16_holiday-sd-2.png' | asset_url }}"/>{% endif %}

 

I'm getting an error as well but it still shows. I can't seem to get the syntax right for it to take the date and do the IF test.

the following were flagged as potential issues in this file:

  • Line 242 — Liquid syntax error: Expected end_of_string but found pipe in "'now' | time_tag: "%Y-%m-%d:%H:%M:%S" >= '2016-12-04:12:00:00'"

These issues may not affect your theme, but should be reviewed if you notice any problems.

 

If you would like to see it, check out the date image on the right lower header: https://shop.nack.is 

 

shipping

 

 

0 Likes
Jason
Shopify Expert
10360 158 2008

Hey Justin,

First some comments:

  1. You can't rely on time being accurate in Liquid. The output of the pages will cached so the value of 'now' will be whenever that cache was made. Do not expect time to be realtime.
  2. You can't run filters within a conditional statement. Make variables to use in the statement.
  3. Expect odd results doing a string comparison with greater / less than. Convert the dates to an integer.

So looking at your code try something more like this:

{% assign cachedNow = 'now' | date:'%Y%m%d%H%M%S' | abs %}
{% assign compareDate = 20161204120000 %}
{%if cachedNow <= compareDate %}
  ...
{% endif %}

 

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
BAToys
Excursionist
16 0 5

Hi Jason,

Totally understand about the cachedNow and compareDate- this would be close enough to what we need.

I am definitely not a coder, but could you help me with a solution that would save us SO MUCH drama?

Looking to set 'variant inventory policy' based on the time check...  is setting "variant inventory policy" to deny or continue allowed via liquid based on this comparison check?

 

EDIT: I am going to try and write something using the metafields we utilize and see if it works.. So when I say help me, I mean help me tweak it, not write it all lol... this is all for naught though if we are unable to set the variant inventory policy in liquid- the values would be pulled from the preloaded metafields... that was my main question I guess, if setting it in liquid is even possible?

0 Likes