Improved post date display.

This commit is contained in:
Cotes Chung 2020-07-23 15:33:42 +08:00
parent 209058b84a
commit b2f0b4cb1b
2 changed files with 31 additions and 26 deletions

View file

@ -28,7 +28,7 @@ layout: default
<span class="author"> <span class="author">
{% if page.author %} {% if page.author %}
{{ page.author }} {{ page.author }}
{% else%} {% else %}
{{ site.author }} {{ site.author }}
{% endif %} {% endif %}
</span> </span>

View file

@ -8,37 +8,41 @@
$(function() { $(function() {
function timeago(date, isLastmod) { function timeago(iso, isLastmod) {
var now = new Date(); let now = new Date();
var past = new Date(date); let past = new Date(iso);
var seconds = Math.floor((now - past) / 1000);
var year = Math.floor(seconds / 31536000); if (past.getFullYear() != now.getFullYear()) {
if (year >= 1) { toRefresh -= 1;
return year + " year" + (year > 1 ? "s" : "") + " ago"; return past.toLocaleString("en-US", {
year: 'numeric',
month: 'short',
day: 'numeric'
});
} }
var month = Math.floor(seconds / 2592000); if (past.getMonth() != now.getMonth()) {
if (month >= 1) { toRefresh -= 1;
return month + " month" + (month > 1 ? "s" : "") + " ago"; return past.toLocaleString("en-US", {
month: 'short',
day: 'numeric'
});
} }
var week = Math.floor(seconds / 604800); let seconds = Math.floor((now - past) / 1000);
if (week >= 1) {
return week + " week" + (week > 1 ? "s" : "") + " ago";
}
var day = Math.floor(seconds / 86400); let day = Math.floor(seconds / 86400);
if (day >= 1) { if (day >= 1) {
toRefresh -= 1;
return day + " day" + (day > 1 ? "s" : "") + " ago"; return day + " day" + (day > 1 ? "s" : "") + " ago";
} }
var hour = Math.floor(seconds / 3600); let hour = Math.floor(seconds / 3600);
if (hour >= 1) { if (hour >= 1) {
return hour + " hour" + (hour > 1 ? "s" : "") + " ago"; return hour + " hour" + (hour > 1 ? "s" : "") + " ago";
} }
var minute = Math.floor(seconds / 60); let minute = Math.floor(seconds / 60);
if (minute >= 1) { if (minute >= 1) {
return minute + " minute" + (minute > 1 ? "s" : "") + " ago"; return minute + " minute" + (minute > 1 ? "s" : "") + " ago";
} }
@ -50,29 +54,30 @@ $(function() {
function updateTimeago() { function updateTimeago() {
$(".timeago").each(function() { $(".timeago").each(function() {
if ($(this).children("i").length > 0) { if ($(this).children("i").length > 0) {
var basic = $(this).text();
var isLastmod = $(this).hasClass('lastmod'); var isLastmod = $(this).hasClass('lastmod');
var node = $(this).children("i"); var node = $(this).children("i");
var date = node.text(); /* ISO Dates: 'YYYY-MM-DDTHH:MM:SSZ' */ var date = node.text(); /* ISO Date: 'YYYY-MM-DDTHH:MM:SSZ' */
$(this).text(timeago(date, isLastmod)); $(this).text(timeago(date, isLastmod));
$(this).append(node); $(this).append(node);
} }
}); });
if (vote == 0 && intervalId != undefined) { if (toRefresh == 0 && intervalId != undefined) {
clearInterval(intervalId); /* stop interval */ clearInterval(intervalId); /* stop interval */
} }
return vote; return toRefresh;
} }
var vote = $(".timeago").length; var toRefresh = $(".timeago").length;
if (vote == 0) {
if (toRefresh == 0) {
return; return;
} }
if (updateTimeago() > 0) { /* run immediately */ if (updateTimeago() > 0) { /* run immediately */
vote = $(".timeago").length; /* resume */ var intervalId = setInterval(updateTimeago, 60000); /* run every minute */
var intervalId = setInterval(updateTimeago, 60000); /* loop every minutes */
} }
}); });