Jekyll 的日期格式是如何工作的?

我在用 Jekyll 创建一个简单的站点。

我希望日期字段以 12 September 2011格式显示。

通过一些有创意的谷歌搜索,我发现了一些日期格式的操作,但似乎没有什么能让我知道月份的名称。我有的是 {{ page.date| date: "%m-%d-%Y" }},它使我的输出为 09-12-2011,但不完全是我所寻找的。

有没有办法在 Jekyll 里把月份作为一个名字?

或者,除此之外,是否有关于 date 属性的文档?

41702 次浏览

Try '%B' which means "The full month name (``January'')"

search the documentation for strftime, the function which is typically used for converting a date to string.

Solution

This output filter:

\{\{ page.date | date: "%-d %B %Y" }}

produces dates formatted like:

9 September 2013

Be sure not to miss the minus (-) in front of %-d for the day. Without it, numbers below ten would have leading zeros (e.g. 09 September 2013).

Details on the individual date formatting tokens can be found on the Liquid "Output tags and filters" documentation page.

More Info

I put together a large set of Jekyll date formatting examples. It provides examples for several formats and should provide enough detail to format in any way you'd like. Some examples include:

  • 2013-09-23
  • September 23, 2013
  • Sept. 23rd, 2013
  • 4 Juli 2013 (i.e. changing names to other languages like "Juli" instead of "July").

Enjoy!

Jekyll adds filter extensions to liquid. See here. You can display your desired date format by simply running the date_to_long_string filter.

From the link:


Date to Long String

Format a date in long format e.g. “27 January 2011”.

\{\{ site.time | date_to_long_string }} => 17 November 2008

Just in case you want a custom solution, you could write a Jekyll plugin to format a date as you want, like this (this example is for Italian dates):

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)

Just save this to a file like _plugins/italian_dates.rb and use it as you need in templates:

<time datetime="\{\{page.date | html5date}}">\{\{page.date | italianDate}}</time>

Use \{\{ page.date| date: "%d %B %Y" }} for dates like 12 September 2011, refer to nice and quick formatting guide on Jekyll Date Formatting

Jekyll 3.8 supports ordinal dates out of the box. To output the month use one of these.

\{\{ page.date | date_to_long_string: "ordinal", "US" }} will output April 24th, 2018.

\{\{ page.date | date_to_string: "ordinal", "US" }} will output Apr 24th, 2018.

\{\{ page.date | date_to_long_string: "ordinal" }} will output 24th April 2018.

\{\{ page.date | date_to_string: "ordinal" }} will output 24th Apr 2018.