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

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