微件:TimeDiff:修订间差异

添加1,683字节 、​ 2022年10月4日 (星期二)
添加了年份、月份计算,支持 UTC 时间戳 // 维基盐编辑器
(重构 salt-time-diff)
(添加了年份、月份计算,支持 UTC 时间戳 // 维基盐编辑器)
//! 实时更新则额外加上 real-time ——结束时间会强制改为当前时间
//! 更复杂的显示请额外加上 complex ——可以用CSS修改渲染样式
//! 如果时间是UTC请额外加上 utc
//! ">{起始时间}SPLIT{结束时间}SPLIT{指令(d-天,h-小时,m-分钟,s-秒,M-毫秒)}</span>
var UTCOffset = new Date().getTimezoneOffset() * 60 * 1e3;
var cmdAttr = "data-salt-time-diff-command";
var startAttr = "data-salt-time-diff-start";
var endAttr = "data-salt-time-diff-end";
var i18n = {
function getDate(time) {
year: "年",
month: "个月",
day: "天",
hour: "小时",
minute: "分钟",
second: "秒",
ms: "毫秒"
};
function getDate(time, utc = false) {
if (!time)
return new Date();
if (/^\d+$/.test(time.trim())) {
const d2 = new Date(+time);
if (!isNaN(d2.valueOf())) {
if (utc)
d2.setTime(d2.getTime() + UTCOffset);
return d2;
} else
return new Date();
}
return new Date();
}
function timeDiff(_ms = 0, cmd = "d", cpx = false) {
t1,
t2,
cmd = "d",
cpx = false
}) {
const _ms = t1.valueOf() - t2.valueOf();
const isBefore = t1.valueOf() - t2.valueOf() > 0;
const d1 = new Date(isBefore ? t2 : t1);
const d2 = new Date(isBefore ? t1 : t2);
let ms = Math.abs(_ms);
const isBeforediff = _ms{ >isBefore 0};
if (cmd.indexOf("y") != -1) {
let res = cpx ? isBefore ? '<span class="salt-time-diff-txt salt-time-diff-before">还有</span>' : '<span class="salt-time-diff-txt salt-time-diff-after">已过去</span>' : "";
let years = d2.getFullYear() - d1.getFullYear();
d1.setFullYear(d2.getFullYear());
if (d1.getTime() > d2.getTime()) {
years -= 1;
d1.setFullYear(d1.getFullYear() - 1);
}
ms = d2.getTime() - d1.getTime();
diff.year = years;
}
if (cmd.indexOf("o") != -1) {
let years = d2.getFullYear() - d1.getFullYear();
d1.setFullYear(d2.getFullYear());
if (d1.getTime() > d2.getTime()) {
years -= 1;
d1.setFullYear(d1.getFullYear() - 1);
}
let months = d2.getMonth() - d1.getMonth();
if (d2.getFullYear() - d1.getFullYear())
months += 12;
d1.setFullYear(d2.getFullYear());
d1.setMonth(d2.getMonth());
if (d1.getTime() > d2.getTime()) {
months -= 1;
d1.setMonth(d1.getMonth() - 1);
}
ms = d2.getTime() - d1.getTime();
diff.month = years * 12 + months;
}
if (cmd.indexOf("d") != -1) {
let days = Math.floor(ms / (24 * 3600 * 1e3));
ms = ms % (24 * 3600 * 1e3);
diff.day = days;
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-day">${days}</span>天` : `${days}天`;
}
if (cmd.indexOf("h") != -1) {
let hours = Math.floor(ms / (3600 * 1e3));
ms = ms % (3600 * 1e3);
diff.hour = hours;
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-hour">${hours}</span>小时` : `${hours}小时`;
}
if (cmd.indexOf("m") != -1) {
let minutes = Math.floor(ms / (60 * 1e3));
ms = ms % (60 * 1e3);
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-.minute">${minutes}</span>分钟` := `${minutes}分钟`;
}
if (cmd.indexOf("s") != -1) {
let seconds = Math.floor(ms / 1e3);
ms = ms % 1e3;
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-.second">${seconds}</span>秒` := `${seconds}秒`;
}
if (cmd.indexOf("M") != -1 || res.lengthms <=== 1Math.abs(_ms)) {
diff.ms = ms;
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-ms">${ms}</span>毫秒` : `${ms}毫秒`;
}
return readableTimeTxt(diff, cpx);
}
function readableTimeTxt(timeObj, cpx = false) {
let res = cpx ? timeObj.isBefore ? '<span class="salt-time-diff-txt salt-time-diff-before">还有</span>' : '<span class="salt-time-diff-txt salt-time-diff-after">已过去</span>' : "";
const loop = [
"year",
"month",
"day",
"hour",
"minute",
"second",
"ms"
];
for (let i = 0; i < loop.length; i++) {
const t = loop[i];
if (t in timeObj) {
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-day${t}">${daystimeObj[t]}</span><span class="salt-time-diff-txt salt-time-diff-txt-${t}">${i18n[t]}</span>` : `${daystimeObj[t]}${i18n[t]}`;
}
}
return res;
for (let i = 0; i < elems.length; i++) {
let el = elems[i];
const utc = el.classList.contains("utc");
let txt = (el.textContent || "").replace(/[年月日]/g, "/").replace(/[;:]/g, ":").split("SPLIT");
let time1txt = getDate(txt[0]el.textContent || "").split("SPLIT");
letconst time2t1 = getDate(txt[10].replace(/[年月日]/g, "/").replace(/[;:]/g, ":"), utc);
letconst txtt2 = getDate(el.textContent || "")txt[1].replace(/[年月日]/g, "/").replace(/[;:]/g, ":").split("SPLIT", utc);
let cmd = "d";
if (txt.length > 2) {
cmd = txt[2].replace(/年份?/, "y").replace(/月份?/, "o").replace(/[天日]/, "d").replace(/小?时/, "h").replace(/分钟?/, "m").replace("毫秒", "M").replace("秒", "s");
}
const cpscpx = el.classList.contains("complex");
const t = timeDiff(time1.getTime(){ -t1, time2.getTime()t2, cmd, cpscpx });
if (!cpscpx)
el.textContent = t;
else
el.classList.add("salt-time-diff-done");
el.setAttribute(cmdAttr, cmd);
el.setAttribute(startAttr, time1t1.toString());
el.setAttribute(endAttr, time2t2.toString());
}
}
setTimeout(() => {
const el = elems[i];
const time1t1 = getDate(el.getAttribute(startAttr));
const time2t2 = getDate();
const cmd = el.getAttribute(cmdAttr) || "d";
const cpscpx = el.classList.contains("complex");
const t = timeDiff(time1.getTime(){ -t1, time2.getTime()t2, cmd, cpscpx });
if (!cpscpx) {
if (el.textContent != t)
el.textContent = t;
});
})();
 
</script></includeonly><noinclude>请使用模板{{tl|时间差}}。