如何将前言中的日期与 site.date 进行比较

How do I compare date in front-matter to site.date

在我的 Jekyll 站点中,我有一个集合 talks,在前面的内容中有一个名为 startDate 的 属性。我在实际的前题中将 startDate 格式化为 %Y-%m-%d,所以我的前题看起来像这样:

---
startDate: 2017-10-29
---

如果 site.time 之后有任何 startDate,我想显示一个名为 即将举行的会谈 的部分。到目前为止我有这个:

{% assign currentDate = site.time | date: "%Y-%m-%d" %}

for 循环中,我可以打印 talk.startDatecurrentDate 并显示每个值,它们在相同的格式中。但我不能做的是比较它们。我真的很想这样做:

{% assign upcoming = site.talks | where_exp: "startDate", "startDate > currentDate" %}`

{% if upcoming.size > 0 %}
  <h1>Upcoming talks</h1>
  ...
{% endif %}

但是当我得到 upcomingpastsize 时(与 upcoming 相同,但 startDate < currentDate 的位置),它们都会计算到 0。这告诉我日期实际上并没有在我的过滤器中进行比较。

谁能帮我解决这个问题?

请阅读此 post:. They try to do the same (only in the past and not in the future). Note that what you are trying to do requires Jekyll to build daily. That is not a very realistic scenario. A 会更好。

javascript 解决方案要求您向列表中添加自定义属性,如下所示:

{% for talk in upcoming %}
  <li date="{{ talk.startDate }}">{{ talk.title }}</li>
{% endfor %}

然后,使用 jQuery(或 vanilla js)隐藏旧的 posts:

// loop through all list items with a date
$('li[date]').each(function(){
  // create a postDate in a date object
  var startDate = new Date($(this).attr('date'));
  // compare dates and hide old posts
  if(startDate<Date.now()) $(this).hide();
});

未说出口的问题是:如何比较 jekyll 或 liquid 中的日期?

这是我能想到的最简单的解决方案:

{%- assign buildTimeInSecondsInEpoch = site.time | date: "%s" | plus: 0 -%}

这将创建一个可以与之进行比较的整数变量。例如:

{%- assign pagesForPublishing = "" | split: "" -%}
{%- for ape in site.pages -%}
    {%- assign pageDateInSecondsInEpoch = ape.date | date:"%s" | plus: 0 -%}
    {%- if pageDateInSecondsInEpoch < buildTimeInSecondsInEpoch -%}
        {%- assign pagesForPublishing = pagesForPublishing | push: ape -%}
    {%- endif -%}
{%- endfor -%}

此外,Jekyll 将不会处理带有未来日期的帖子,以允许使用 --future 选项(在构建或服务时)。

无论日期如何,页面文件都将由 jekyll 处理。