比较液体中的日期 [英] Comparing dates in liquid

查看:87
本文介绍了比较液体中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Liquid with Jekyll to post dates on my band's website (http://longislandsound.com.au)

我想要的是自动隐藏旧的日期,因此我不必再次删除它们.我认为最好的方法是将发布日期与当前日期进行比较,并且仅在该日期为将来时才显示该帖子,但是我不知道该怎么做.

What I want is to automatically hide old dates, so I don't have to go in and delete them again. I think the best way to do it would be to compare the post date to the current date and only display the post if the date is in the future, but I can't figure out how to do this.

这是当前代码:

<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>

我尝试了一些if语句,但是我不知道如何比较发布日期和当前日期.

I've tried some if statements, but I can't figure out how to compare the post date to the current date.

任何人都可以帮忙吗?

推荐答案

基于 date-math-manipulation-in-liquid-template-filter

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 %}

这篇关于比较液体中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆