我正在使用Jekyll生成一个简单的网站。
我想将日期字段显示为格式为2011年9月12日
。
我通过一些创意的谷歌搜索找到了一些日期格式操作,但没有找到任何可以获取月份名称的内容。我现在有的是{{ page.date| date: "%Y年%m月%d日" }}
,它会输出2011年09月12日
,但这不完全是我想要的。
是否有方法可以在Jekyll中以名称形式获取月份?
或者,如果没有,是否有关于日期属性的任何文档?
这个输出过滤器:
{{ page.date | date: "%-d %B %Y" }}
生成格式如下的日期:
9 September 2013
请确保在日期格式中的天数格式化标记%-d
前不要漏掉减号-
。如果没有它,数字小于十的日期将具有前导零,例如09 September 2013
。plus: '0'
。请使用:{{ page.date | date: "%-m-%d-%Y" }}
。注意 %
和 m
之间的连字符:%-m
。 - Nowaker%-m
的方法。这样更加简洁。我已经更新了答案并将尽快更新我的参考文章。 - Alan W. SmithJekyll为liquid添加了过滤器扩展。请参见此处。只需运行date_to_long_string
过滤器,即可显示所需的日期格式。
来自链接:
将日期格式化为长格式(如“2011年1月27日”)。
{{ site.time | date_to_long_string }}
=> 2008年11月17日
Jekyll 3.8支持基于序号的日期格式。以下是输出月份的方法:
{{ page.date | date_to_long_string: "ordinal", "US" }}
将输出April 24th, 2018
。
{{ page.date | date_to_string: "ordinal", "US" }}
将输出Apr 24th, 2018
。
{{ page.date | date_to_long_string: "ordinal" }}
将输出24th April 2018
。
{{ page.date | date_to_string: "ordinal" }}
将输出24th Apr 2018
。
如果您需要自定义解决方案,可以编写一个Jekyll插件来按照您想要的格式格式化日期,例如(以下示例适用于意大利日期):
module Jekyll
module ItalianDates
MONTHS = {"01" => "gennaio", "02" => "febbraio", "03" => "marzo",
"04" => "aprile", "05" => "maggio", "06" => "giugno",
"07" => "luglio", "08" => "agosto", "09" => "settembre",
"10" => "ottobre", "11" => "novembre", "12" => "dicembre"}
# http://man7.org/linux/man-pages/man3/strftime.3.html
def italianDate(date)
day = time(date).strftime("%e") # leading zero is replaced by a space
month = time(date).strftime("%m")
year = time(date).strftime("%Y")
day+' '+MONTHS[month]+' '+year
end
def html5date(date)
day = time(date).strftime("%d")
month = time(date).strftime("%m")
year = time(date).strftime("%Y")
year+'-'+month+'-'+day
end
end
end
Liquid::Template.register_filter(Jekyll::ItalianDates)
只需将其保存到文件中,例如_plugins/italian_dates.rb
,然后在模板中根据需要使用:
<time datetime="{{page.date | html5date}}">{{page.date | italianDate}}</time>
date_to_string
和date_to_xmlschema
(参见http://jekyllrb.com/docs/templates/)。 - Martin Thoma