date 将日期和时间按指定格式输出
将时间戳转换成另一种日期格式,格式和 strftime
一样,输入的格式和 Ruby 的 Time.parse
一样。
输入
{{ article.published_at | date: "%a, %b %d, %y" }}
输出
Fri, Jul 17, 15
输入
{{ article.published_at | date: "%Y" }}
输出
2015
如果字符串包含正确格式的日期,date
过滤器可以对它起作用,会按照字符串的格式输出日期。
输入
{{ "March 14, 2016" | date: "%b %d, %y" }}
输出
Mar 14, 16
要获取当前时间,可以将特殊的词now
(或today
)传递给 date
。
- now 现在时间
- today 今天日期
输入
This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}.
输出
This page was last updated at 2025-03-17 03:02.
需要注意的是,显示的时间是页面最后一次生成时的时间,而不是页面展示给用户时的时间,尤其是在涉及缓存或生成静态页面时。