Jekyll custom date

2019-08-01 00:10发布

问题:

I want use bellow code to display custom date in my Jekyll site

{% assign m = page.date | date: "%-m" %}
{% case m %}
{% when '1' %}Januar
{% when '2' %}Februar
{% when '3' %}März
{% when '4' %}April
{% when '5' %}Mai
{% when '6' %}Juni
{% when '7' %}Juli
{% when '8' %}August
{% when '9' %}September
{% when '10' %}Oktober
{% when '11' %}November
{% when '12' %}Dezember
{% endcase %}

But I don't now where to put it (I tried in post.html but does not work)

回答1:

I've made a template for this. This template translate a date in a specific language. Here it's french but feel free to change month and day arrays. This template can be used in an enumeration of post/page (eg: the index page) or in a post/page template.

When used in an enumeration, you need to pass the date to process

{% for post in site.posts %}
  <li>
    <span class="post-date">{% include custom_date_full_fr.html date = post.date %}</span>
    <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
  </li>
{% endfor %}

Where used in a page/post template, you just have to include the template, as the page.date will already be available.

{% include custom_date_full_fr.html %}

custom_date_full_fr.html

{% if include.date %}
    {% assign processed_date = include.date %}
{% else if page.date  %}
    {% assign processed_date = page.date %}
{% endif %}

{% comment %}-------- Test if we have a date to process --------{% endcomment %}
{% if processed_date %}

    {% assign month = "janvier,février,mars,avril,mai,juin,juillet,août,septembre,octobre,novembre,décembre" | split: "," %}

    {% comment %}------ Note : sunday is the first day in this array -------{% endcomment %}
    {% assign day = "dimanche,lundi,mardi,mercredi,jeudi,vendredi,samedi" | split: "," %}

    {% assign month_index = processed_date | date: "%m" | minus: 1 %}

    {%comment%}----------------------------------------------
    Here **minus: 0** is a trick to convert day_index from string to integer and then use it as an array index.
    ----------------------------------------------{%endcomment%}
    {% assign day_index = processed_date | date: "%w" | minus: 0 %}

    {%comment%}-------- Output the date ----------{%endcomment%}
    {{ day[day_index] }} {{ processed_date | date: "%d" }} {{ month[month_index] }} {{ processed_date | date: "%Y" }}
{% endif %}

See here for more info :

  • Jekyll Date Formatting Examples by Alan W. Smith
  • Liquid documentation - date filters