Skip to content

Commit 66b16a8

Browse files
committed
Improved post date display.
1 parent 411fb94 commit 66b16a8

2 files changed

Lines changed: 32 additions & 27 deletions

File tree

_layouts/post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1 data-toc-skip>{{ page.title }}</h1>
2828
<span class="author">
2929
{% if page.author %}
3030
{{ page.author }}
31-
{% else%}
31+
{% else %}
3232
{{ site.author }}
3333
{% endif %}
3434
</span>

assets/js/_utils/timeago.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,41 @@
88

99
$(function() {
1010

11-
function timeago(date, isLastmod) {
12-
var now = new Date();
13-
var past = new Date(date);
14-
var seconds = Math.floor((now - past) / 1000);
15-
16-
var year = Math.floor(seconds / 31536000);
17-
if (year >= 1) {
18-
return year + " year" + (year > 1 ? "s" : "") + " ago";
11+
function timeago(iso, isLastmod) {
12+
let now = new Date();
13+
let past = new Date(iso);
14+
15+
if (past.getFullYear() != now.getFullYear()) {
16+
toRefresh -= 1;
17+
return past.toLocaleString("en-US", {
18+
year: 'numeric',
19+
month: 'short',
20+
day: 'numeric'
21+
});
1922
}
2023

21-
var month = Math.floor(seconds / 2592000);
22-
if (month >= 1) {
23-
return month + " month" + (month > 1 ? "s" : "") + " ago";
24+
if (past.getMonth() != now.getMonth()) {
25+
toRefresh -= 1;
26+
return past.toLocaleString("en-US", {
27+
month: 'short',
28+
day: 'numeric'
29+
});
2430
}
2531

26-
var week = Math.floor(seconds / 604800);
27-
if (week >= 1) {
28-
return week + " week" + (week > 1 ? "s" : "") + " ago";
29-
}
32+
let seconds = Math.floor((now - past) / 1000);
3033

31-
var day = Math.floor(seconds / 86400);
34+
let day = Math.floor(seconds / 86400);
3235
if (day >= 1) {
36+
toRefresh -= 1;
3337
return day + " day" + (day > 1 ? "s" : "") + " ago";
3438
}
3539

36-
var hour = Math.floor(seconds / 3600);
40+
let hour = Math.floor(seconds / 3600);
3741
if (hour >= 1) {
3842
return hour + " hour" + (hour > 1 ? "s" : "") + " ago";
3943
}
4044

41-
var minute = Math.floor(seconds / 60);
45+
let minute = Math.floor(seconds / 60);
4246
if (minute >= 1) {
4347
return minute + " minute" + (minute > 1 ? "s" : "") + " ago";
4448
}
@@ -50,29 +54,30 @@ $(function() {
5054
function updateTimeago() {
5155
$(".timeago").each(function() {
5256
if ($(this).children("i").length > 0) {
57+
var basic = $(this).text();
5358
var isLastmod = $(this).hasClass('lastmod');
5459
var node = $(this).children("i");
55-
var date = node.text(); /* ISO Dates: 'YYYY-MM-DDTHH:MM:SSZ' */
60+
var date = node.text(); /* ISO Date: 'YYYY-MM-DDTHH:MM:SSZ' */
5661
$(this).text(timeago(date, isLastmod));
5762
$(this).append(node);
5863
}
5964
});
6065

61-
if (vote == 0 && intervalId != undefined) {
66+
if (toRefresh == 0 && intervalId != undefined) {
6267
clearInterval(intervalId); /* stop interval */
6368
}
64-
return vote;
69+
return toRefresh;
6570
}
6671

6772

68-
var vote = $(".timeago").length;
69-
if (vote == 0) {
73+
var toRefresh = $(".timeago").length;
74+
75+
if (toRefresh == 0) {
7076
return;
7177
}
7278

73-
if (updateTimeago() > 0) { /* run immediately */
74-
vote = $(".timeago").length; /* resume */
75-
var intervalId = setInterval(updateTimeago, 60000); /* loop every minutes */
79+
if (updateTimeago() > 0) { /* run immediately */
80+
var intervalId = setInterval(updateTimeago, 60000); /* run every minute */
7681
}
7782

7883
});

0 commit comments

Comments
 (0)