比较液体中的日期

发布于 2024-11-29 23:36:12 字数 884 浏览 7 评论 0原文

我正在使用 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>

我尝试了一些 if 语句,但我不知道如何将发布日期与当前日期进行比较。

有人可以帮忙吗?

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.

Here is the current code:

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

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

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-12-06 23:36:12

基于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...

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

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

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 %}
花海 2024-12-06 23:36:12

尽管此代码有效:

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

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

从这个 Liquid 开始:

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

并添加这个 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(); 
});

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


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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文