获取杰基尔两个日期之间的天数差异 [英] Get the difference in days between two dates in Jekyll

查看:85
本文介绍了获取杰基尔两个日期之间的天数差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到Jekyll中两个日期之间天数的差异.我该如何实现?

I would like to get the difference in days between two dates in Jekyll. How can I achieve this?

{% capture currentDate %}{{ site.time | date: '%Y-%m-%d' }}{% endcapture %}
{{currentDate}}
{% capture event_date %}{{ entry.date }}{% endcapture %}
{% if event_date < currentDate %}Yes{% else %}No{% endif %}

在条目中有我的YAML:

In the entry there is my YAML:

---
title: ChartLine C3
type: charts
description: Chart with round for prisma
id: c3-1
date: 2015-07-18
--- 

推荐答案

如果您只想知道Front Matter中的日期是否早于系统时间,则可以使用ISO 8601日期格式并依靠字典顺序.有点作弊,但可以在您提供的示例中使用.

If all you want to do is know whether a date from your Front Matter is earlier than the system time then you can use the ISO 8601 date format and rely on the lexicographical ordering. It's sort of cheating but it'll work for the example you provided.

重要的是要以ISO 8601格式捕获两者 site.time和您的前台事务(以下示例中为page.past_datepage.future_date)中的日期,以便此技巧起作用.

It's important to capture both site.time and the date from your Front Matter (page.past_date and page.future_date in the example below) in ISO 8601 format in order for this trick to work.

---
layout: default
past_date: 2015-03-02
future_date: 2016-03-02
---

{% capture currentDate %}{{ site.time | date: '%F' }}{% endcapture %}
{% capture pastDate %}{{ page.past_date | date: '%F' }}{% endcapture %}
{% capture futureDate %}{{ page.future_date | date: '%F' }}{% endcapture %}
<br>currentDate: {{currentDate}}
<br>PastDate earlier than currentDate? {% if pastDate < currentDate %}Yes{% else %}No{% endif %}
<br>FutureDate earlier than currentDate? {% if futureDate < currentDate %}Yes{% else %}No{% endif %}

给我以下输出:

currentDate:2015-07-12

currentDate: 2015-07-12

PastDate早于currentDate?是

PastDate earlier than currentDate? Yes

FutureDate早于currentDate?不

FutureDate earlier than currentDate? No

这篇关于获取杰基尔两个日期之间的天数差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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