Comparing dates in liquid

ε祈祈猫儿з 提交于 2019-12-03 05:39:00
here

Based on date-math-manipulation-in-liquid-template-filter and get-todays-date-in-jekyll-with-liquid-markup, you should be able to use a combination of {{'now'}} or {{site.time}} and the hard to find unix timestamp date filter | date: '%s'

{% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
{% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
{% if posttime < nowunix %}
...show posts...

Captured numbers may act as strings, not numbers, and can be type cast back to numbers using the following hack:

{% assign nowunix = nowunix | plus: 0 %}

Although this code works:

{% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
{% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
{% if posttime < nowunix %} ...show posts...

It only is executed during build. If you want your website to truly update automatically, you should let javascript do the hiding.

Start with this Liquid:

{% for item in site.events %} 
  <div future-date="{{ item.date | date: '%Y%m%d' }}">...</div> 
{% endfor %}

And add this javascript:

function getCompareDate() { 
  var d = new Date(), 
      month = '' + (d.getMonth() + 1), 
      day = '' + d.getDate(), 
      year = d.getFullYear(); 
  if (month.length < 2) month = '0' + month; 
  if (day.length < 2) day = '0' + day; 
  return [year, month, day].join(''); 
} 

$('[future-date]').each(function() { 
  if($(this).attr('future-date') < getCompareDate()) $(this).hide(); 
});

The solution was found here: http://jekyllcodex.org/without-plugin/future-dates/


UPDATE (2018-02-19):
CloudCannon now has scheduled builds where you can simply specify to build your project once a day. If you use CloudCannon, I recommend the answer of the user [here].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!