Create a gist now

Instantly share code, notes, and snippets.

@hail2u /reldate.js
Last active Aug 7, 2017

What would you like to do?
datetime属性を持つtime要素を全部相対時刻にするやつ
/*!
* reldate.js
*
* LICENSE: http://hail2u.mit-license.org/2016
*/
"use strict";
(function () {
const now = Date.now();
const toRelativeDate = function (then) {
let diff = 0;
if (!Number.isInteger(then)) {
return;
}
diff = parseInt((now - then) / 1000, 10);
if (diff < 0) {
return;
}
if (diff < 60) {
return diff + "秒前";
}
diff = parseInt(diff / 60, 10);
if (diff < 60) {
return diff + "分前";
}
diff = parseInt(diff / 60, 10);
if (diff < 24) {
return diff + "時間前";
}
diff = parseInt(diff / 24, 10);
if (diff < 30) {
return diff + "日前";
}
diff = parseInt(diff / 30, 10);
if (diff < 12) {
return diff + "ヶ月前";
}
return parseInt(diff / 12, 10) + "年前";
};
for (let time of document.querySelectorAll("time")) {
let abs = time.getAttribute("datetime");
let rel = "";
if (!abs) {
continue;
}
rel = toRelativeDate(Date.parse(abs));
// 後でまとめてやりたい
if (rel) {
time.setAttribute("title", abs);
time.textContent = rel;
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment