113 řádky
2.6 KiB
JavaScript
113 řádky
2.6 KiB
JavaScript
$(function () {
|
|
update_reltimes($("time"));
|
|
|
|
setInterval(() => {
|
|
update_reltimes($("time"));
|
|
}, 60000);
|
|
});
|
|
|
|
function update_reltimes(elements) {
|
|
elements.each(function () {
|
|
let title = $(this).attr("title");
|
|
|
|
if (!title) {
|
|
$(this).prop("title", $(this).text());
|
|
}
|
|
|
|
let rel = reltime($(this).attr("datetime"));
|
|
|
|
$(this).text(rel);
|
|
});
|
|
}
|
|
|
|
const MINUTE = 60000,
|
|
HOUR = 3600000,
|
|
DAY = 86400000,
|
|
WEEK = 604800000,
|
|
MONTH = 2592000000,
|
|
YEAR = 31536000000;
|
|
|
|
function reltime(date) {
|
|
let delta = Date.now() - Date.parse(date);
|
|
let fut = false;
|
|
|
|
if (delta < 0) {
|
|
delta = Math.abs(delta);
|
|
fut = true;
|
|
}
|
|
|
|
let minutes = Math.floor(delta / MINUTE);
|
|
let hours = Math.floor(delta / HOUR);
|
|
let days = Math.floor(delta / DAY);
|
|
let weeks = Math.floor(delta / WEEK);
|
|
let months = Math.floor(delta / MONTH);
|
|
let years = Math.floor(delta / YEAR);
|
|
|
|
let rt = "Teď";
|
|
|
|
if (minutes > 0) {
|
|
if (fut) {
|
|
rt = `za ${minutes} ${plural("minutu|minuty|minut", minutes)}`;
|
|
} else {
|
|
rt = `před ${minutes} ${plural("minutou|minutami|minutami", minutes)}`;
|
|
}
|
|
}
|
|
|
|
if (hours > 0) {
|
|
if (fut) {
|
|
rt = `za ${hours} ${plural("hodinu|hodiny|hodin", hours)}`;
|
|
} else {
|
|
rt = `před ${hours} ${plural("hodinou|hodinami|hodinami", hours)}`;
|
|
}
|
|
}
|
|
|
|
if (days > 0) {
|
|
if (fut) {
|
|
rt = `za ${days} ${plural("den|dny|dnů", days)}`;
|
|
} else {
|
|
rt = `před ${days} ${plural("dnem|dny|dny", days)}`;
|
|
}
|
|
}
|
|
|
|
if (weeks > 0) {
|
|
if (fut) {
|
|
rt = `za ${weeks} ${plural("týden|týdny", weeks)}`;
|
|
} else {
|
|
rt = `před ${weeks} ${plural("týdnem|týdny", weeks)}`;
|
|
}
|
|
}
|
|
|
|
if (months > 0) {
|
|
if (fut) {
|
|
rt = `za ${months} ${plural("měsíc|měsíce|měsíců", months)}`;
|
|
} else {
|
|
rt = `před ${months} ${plural("měsícem|měsíci|měsíci", months)}`;
|
|
}
|
|
}
|
|
|
|
if (years > 0) {
|
|
if (fut) {
|
|
rt = `za ${years} ${plural("rok|roky|let", years)}`;
|
|
} else {
|
|
rt = `před ${years} ${plural("rokem|lety|lety", years)}`;
|
|
}
|
|
}
|
|
|
|
return rt;
|
|
}
|
|
|
|
function plural(plurals, count) {
|
|
let plurals_arr = plurals.split("|");
|
|
let one = plurals_arr[0];
|
|
let few = plurals_arr[1];
|
|
let other = plurals_arr[2];
|
|
|
|
if (count === 1) {
|
|
return one;
|
|
} else if (count < 5 && count !== 0) {
|
|
return few;
|
|
} else {
|
|
return other;
|
|
}
|
|
}
|