比较液体的日期

Chr*_*nce 13 templates date liquid jekyll

我正在使用Liquid和Jekyll在我的乐队网站上发布日期(http://longislandsound.com.au)

我想要的是自动隐藏旧日期,所以我不必再进去删除它们.我认为最好的方法是将发布日期与当前日期进行比较,如果日期是将来只显示帖子,但我无法弄清楚如何做到这一点.

这是当前的代码:

<ul id="dates">
{% for post in site.posts reversed %}
<a href="{{post.link}}">
<li>
    <div class="date">
        <span class="day">{{post.date | date: "%d"}}</span><br />
        <span class="month">{{post.date | date: "%b"}}</span>
        <span class="week">{{post.date | date: "%a"}}</span>
    </div>
    <div class="details">
        <span class="venue">{{post.venue}}</span><br />
        <span class="town">{{post.town}}</span>
    </div>
</li>
</a>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一些if语句,但我无法弄清楚如何将发布日期与当前日期进行比较.

有人可以帮忙吗?

her*_*ere 29

基于date-math-manipulation-in-liquid-template-filterget-todays-date-in-jekyll-with-liquid-markup,你应该能够使用{{'now'}}或者{{site.time}}很难找到unix时间戳日期的组合过滤| date: '%s'

{% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
{% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
{% if posttime < nowunix %}
...show posts...
Run Code Online (Sandbox Code Playgroud)

捕获的数字可以充当字符串,而不是数字,并且可以使用以下hack类型转换回数字:

{% assign nowunix = nowunix | plus: 0 %}
Run Code Online (Sandbox Code Playgroud)


Joo*_*stS 5

虽然此代码有效:

{% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
{% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
{% if posttime < nowunix %} ...show posts...
Run Code Online (Sandbox Code Playgroud)

它仅在构建期间执行。如果您希望您的网站真正自动更新,您应该让 javascript 进行隐藏。

从这个液体开始:

{% for item in site.events %} 
  <div future-date="{{ item.date | date: '%Y%m%d' }}">...</div> 
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

并添加这个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(); 
});
Run Code Online (Sandbox Code Playgroud)

解决方案在这里找到:http : //jekyllcodex.org/without-plugin/future-dates/


更新 (
2018-02-19 ): CloudCannon 现在已经安排了构建,您可以简单地指定每天构建一次项目。如果您使用 CloudCannon,我推荐用户[此处]的回答。