How to use build_date_utc for copyright year in mkdocs.yml

Viewed 321

In my project, I want to use the current year for copyright information in mkdocs config file mkdocs.yml. According to the mkdocs documentation there is build_date_utc. Could someone give an example of how to use it? I tried

copyright: "&copy; 2017 - {{ build_date_utc.year }} <a href='https://example.org' target='_blank'>Example</a>"

But the parameter does not render correctly in HTML.

Update: I found out that it works with a JavaScript script, like this:

copyright: "&copy; 2017 - <script>document.write(new Date().getFullYear())</script> <a href='https://example.org' target='_blank'>Example</a>"

But I would still be interested in how to do it with build_date_utc.

1 Answers

According the the MkDoc docs that you linked to build_date_utc is:

A Python datetime object that represents the date and time the documentation was built in UTC.

Since it is a Python datetime object you can format it with strftime like any other Python datetime object.

This works for me, using mkdocs 1.2.1 and jinja2 3.0.1:

{% block footer %}
 &copy; {{ build_date_utc.strftime('%Y') }} 
 <a href='https://example.org' target='_blank'>Example</a>
{% endblock %}
Related