Liquid/Jekyll中的序数日期格式(例如“第1个",“第3个"和“第4个") [英] Ordinalize date formatting in Liquid/Jekyll (e.g. "1st", "3rd" and "4th")

查看:90
本文介绍了Liquid/Jekyll中的序数日期格式(例如“第1个",“第3个"和“第4个")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Liquid或Jekyll中将日期的后缀添加到日期格式?例如:1月23日或5月18日.

Is it possible to add day of the month suffixes to a date format in Liquid or Jekyll? For example: January 23rd or May 18th.

我已经提到了 Shopify Wiki ,但我很震惊地发现没有格式设置在那里.当然应该这么简单吗? :/

I've referred to the Shopify wiki, but I'm shocked to see that there's no formatting there for it. Surely something that simple should be? :/

推荐答案

Jekyll 使用的Liquid Template Engine无法提供以下功能:开箱即用(例如,将"1"变成"1st",将"3"变成"3rd").但是,可以使用过滤器和标签来提供该功能.下面的代码段生成了每月的日期编号,并附加了经过丹尼尔化的字符串.它还会删除该月前九天的前导零.

The Liquid Template Engine that Jekyll uses doesn't offer the ability to ordinalize (e.g. turn "1" into "1st" and "3" into "3rd") out of the box. However, it is possible to use filters and tags to provide that functionality. The snippet below produces the day of month number with an ordanilized string appended. It also removes the leading zero for the first nine days of the month.

{% assign d = page.date | date: "%-d" %}
{% case d %}
  {% when "1" or "21" or "31" %}{{ d }}st
  {% when "2" or "22" %}{{ d }}nd
  {% when "3" or "23" %}{{ d }}rd
  {% else %}{{ d }}th
{% endcase %}

要获取包含月,日和年的完整日期,请使用以下方法:

For a full date with month, day and year, use this:

{% assign d = page.date | date: "%-d" %}
{{ page.date | date: "%B" }} 
{% case d %}{% when "1" or "21" or "31" %}{{ d }}st{% when "2" or "22" %}{{ d }}nd{% when "3" or "23" %}{{ d }}rd{% else %}{{ d }}th{% endcase %}, 
{{ page.date | date: "%Y" }}

产生如下输出:

September 21st, 2013

注意:该代码分为多行,以使其更易于阅读.它可以用HTML很好地呈现,但是在源代码中会有额外的空格.如果那样困扰您,只需将所有内容移到一行即可.

如果您对其他日期格式选项感兴趣,请创建此引用: Jekyll(和GitHub Pages)临时日期格式示例

If you are interested in other date formatting options, I create this reference: Jekyll (and GitHub Pages) Liquid Date Formatting Examples

这篇关于Liquid/Jekyll中的序数日期格式(例如“第1个",“第3个"和“第4个")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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