Auto-adjust publish and update timestamps via JS

This commit is contained in:
Jonas Tobias Hopusch 2023-03-10 16:24:44 +01:00
parent 4bdbf2a598
commit 07b85f19ec
Signed by: jotoho
GPG key ID: 913BDF1196DCF600
2 changed files with 13 additions and 2 deletions

View file

@ -0,0 +1,10 @@
"use strict";
Array.from(document.getElementsByClassName("relative-timestamp")).forEach(
function (timestamp) {
timestamp.innerText = new Intl.DateTimeFormat(undefined, {
dateStyle: "long",
timeStyle: "short",
}).format(new Date(timestamp.dateTime));
}
);

View file

@ -1,6 +1,7 @@
{% extends "base.html" %} {% block metadata %} {% extends "base.html" %} {% block metadata %}
<meta name="description" content="{{ page.description }}" /> <meta name="description" content="{{ page.description }}" />
<script defer src="/scripts/relativetime.js"></script>
{% endblock %} {% block content %} {% endblock %} {% block content %}
<h2>{{ page.title }}</h2> <h2>{{ page.title }}</h2>
@ -8,7 +9,7 @@
{% if page.date and page.date is defined %} {% if page.date and page.date is defined %}
<p> <p>
<span class="article-metadata-label">First published</span>: <span class="article-metadata-label">First published</span>:
<time datetime="{{page.date}}" <time class="relative-timestamp" datetime="{{page.date}}"
>{{ page.date | date(format="%F %R (%Z)", timezone="Europe/Berlin") >{{ page.date | date(format="%F %R (%Z)", timezone="Europe/Berlin")
}}</time }}</time
> >
@ -16,7 +17,7 @@
{% endif %} {% if page.updated and page.updated != page.date %} {% endif %} {% if page.updated and page.updated != page.date %}
<p> <p>
<span class="article-metadata-label">Last updated on</span>: <span class="article-metadata-label">Last updated on</span>:
<time datetime="{{page.updated}}" <time class="relative-timestamp" datetime="{{page.updated}}"
>{{ page.updated | date(format="%F %R (%Z)", timezone="Europe/Berlin") >{{ page.updated | date(format="%F %R (%Z)", timezone="Europe/Berlin")
}}</time }}</time
> >