Copy link to clipboard
Copied
Hey BC Community... wondering if anyone can offer some help with the Liquid Date now?
Want to show if the office is closed during a certain time period, that it would output:
- OPEN - for office times of 10am - 4pm
- CLOSED - for office times of 4:01pm - 9:59am
Any suggestions? Still a little green when it comes to liquid, so any code help would be much appreciated. Thanks!
Aaron
Copy link to clipboard
Copied
Use JavaScript for this purpose, in my opinion.
Just know that both BC's liquid date "{{
globals.site.dateNow }}
" and JavaScript's "Date.now()
" uses the visitors local current time and not the time in your customer's local timezone. So using JS Date.now() you can find the timezone offset using ".getTimezoneOffset()" and work out what the current time is for the local business. Then apply classes for Open / Closed states.
You could use Liquid however I'd suggest placing this calculation overhead onto the visitor using JS.
Copy link to clipboard
Copied
The above actually is not correct and You should use liquid for this not javascript.
Copy link to clipboard
Copied
Tip: use 24-hour format for the calculation.
E.G.:
{% assign hour = globals.site.dateNow | date: "HH" -%}
{% if hour > 10 and hour < 16 -%}
OPEN
{% else -%}
CLOSED
{% endif -%}
Update: Convert your work hours to UTC format and then use date_utc liquid filter to map visitor's timezone with your office time. So above assign would be:
{% assign hour = globals.site.dateNow | date_utc | date: "HH" -%}
Copy link to clipboard
Copied
This is awesome! Thanks UPGR8R and all!
Copy link to clipboard
Copied
I am also a newbie with liquid. I have an event with early bird pricing and I want to check if the current date is before the early bird date (so I can only show discount pricing). I've got this far but I think my assignment for Mydate is wrong. The webapp item is called eb-date. Any help appreciated.
{% assign {eb-date}= myDate-%}
{% if globals.site.dateNow < myDate %}
<p>The date is in the future</p>
{%else %}
<p>The date is in the past</p> {% endif %}
Copy link to clipboard
Copied
When you're placing a liquid tag, you use double curly brackets to denote a liquid variable, such as {{ eb-date }}.
However, when using liquid logic, such as {% assign -%} or {% if -%}, you don't need to encapsulate your variables in brackets.
So, I think your solution is:
{% assign eb-date = myDate -%}
{% if globals.site.dateNow < myDate -%}
<p>The date is in the future</p>
{% else -%}
<p>The date is in the past</p>
{% endif -%}
Tip: Be more pedantic with your spaces between the liquid tags - BC's rendition of liquid can be unforgiving sometime.
Copy link to clipboard
Copied
Thanks Stephen
I actually got it to work by just using the liquid tag (as you suggested). This seems to work but I will try it without braces.
{% if globals.site.dateNow < {eb-date} %}
<p>The date is in the future</p>
{%else %}
<p>The date is in the past</p> {% endif %}