MCBBS Wiki欢迎您共同参与编辑!在参与编辑之前请先阅读Wiki方针

如果在编辑的过程中遇到了什么问题,可以去讨论板提问。

为了您能够无阻碍地参与编辑 未验证/绑定过邮箱的用户,请尽快绑定/验证

MCBBS Wiki GitHub群组已上线!

您可以在回声洞中发表吐槽!

服务器状态监控。点击进入

本站由MCBBS用户自行搭建,与MCBBS及东银河系漫游指南(北京)科技有限公司没有从属关系。点此了解 MCBBS Wiki 不是什么>>

微件:TimeDiff:修订间差异

来自MCBBS Wiki
跳到导航 跳到搜索
(添加了年份、月份计算,支持 UTC 时间戳 // 维基盐编辑器)
(添加简化输出模式)
 
第16行: 第16行:
//! 更复杂的显示请额外加上 complex ——可以用CSS修改渲染样式
//! 更复杂的显示请额外加上 complex ——可以用CSS修改渲染样式
//! 如果时间是UTC请额外加上 utc
//! 如果时间是UTC请额外加上 utc
//! 简化输出请额外加上 simple
//! ">{起始时间}SPLIT{结束时间}SPLIT{指令(d-天,h-小时,m-分钟,s-秒,M-毫秒)}</span>
//! ">{起始时间}SPLIT{结束时间}SPLIT{指令(d-天,h-小时,m-分钟,s-秒,M-毫秒)}</span>
var UTCOffset = new Date().getTimezoneOffset() * 60 * 1e3;
var UTCOffset = new Date().getTimezoneOffset() * 60 * 1e3;
第52行: 第53行:
t2,
t2,
cmd = "d",
cmd = "d",
cpx = false
cpx = false,
simple = false
}) {
}) {
const _ms = t1.valueOf() - t2.valueOf();
const _ms = t1.valueOf() - t2.valueOf();
第68行: 第70行:
}
}
ms = d2.getTime() - d1.getTime();
ms = d2.getTime() - d1.getTime();
diff.year = years;
if (!simple || years)
diff.year = years;
}
}
if (cmd.indexOf("o") != -1) {
if (cmd.indexOf("o") != -1) {
第87行: 第90行:
}
}
ms = d2.getTime() - d1.getTime();
ms = d2.getTime() - d1.getTime();
diff.month = years * 12 + months;
if (!simple || months || "year" in diff)
diff.month = years * 12 + months;
}
}
if (cmd.indexOf("d") != -1) {
if (cmd.indexOf("d") != -1) {
let days = Math.floor(ms / (24 * 3600 * 1e3));
let days = Math.floor(ms / (24 * 3600 * 1e3));
ms = ms % (24 * 3600 * 1e3);
ms = ms % (24 * 3600 * 1e3);
if (!simple || days || "year" in diff || "month" in diff)
diff.day = days;
diff.day = days;
}
}
if (cmd.indexOf("h") != -1) {
if (cmd.indexOf("h") != -1) {
let hours = Math.floor(ms / (3600 * 1e3));
let hours = Math.floor(ms / (3600 * 1e3));
ms = ms % (3600 * 1e3);
ms = ms % (3600 * 1e3);
diff.hour = hours;
if (!simple || hours || "day" in diff)
diff.hour = hours;
}
}
if (cmd.indexOf("m") != -1) {
if (cmd.indexOf("m") != -1) {
let minutes = Math.floor(ms / (60 * 1e3));
let minutes = Math.floor(ms / (60 * 1e3));
ms = ms % (60 * 1e3);
ms = ms % (60 * 1e3);
diff.minute = minutes;
if (!simple || minutes || "hour" in diff)
diff.minute = minutes;
}
}
if (cmd.indexOf("s") != -1) {
if (cmd.indexOf("s") != -1) {
let seconds = Math.floor(ms / 1e3);
let seconds = Math.floor(ms / 1e3);
ms = ms % 1e3;
ms = ms % 1e3;
diff.second = seconds;
if (!simple || seconds || "minute" in diff)
diff.second = seconds;
}
}
if (cmd.indexOf("M") != -1 || ms === Math.abs(_ms)) {
if (cmd.indexOf("M") != -1 || ms === Math.abs(_ms)) {
第145行: 第153行:
cmd = txt[2].replace(/年份?/, "y").replace(/月份?/, "o").replace(/[天日]/, "d").replace(/小?时/, "h").replace(/分钟?/, "m").replace("毫秒", "M").replace("秒", "s");
cmd = txt[2].replace(/年份?/, "y").replace(/月份?/, "o").replace(/[天日]/, "d").replace(/小?时/, "h").replace(/分钟?/, "m").replace("毫秒", "M").replace("秒", "s");
}
}
const cpx = el.classList.contains("complex");
const t = timeDiff({ t1, t2, cmd, cpx });
if (!cpx)
el.textContent = t;
else
el.innerHTML = t;
el.classList.remove("salt-time-diff");
el.classList.remove("salt-time-diff");
el.classList.add("salt-time-diff-done");
el.classList.add("salt-time-diff-done");
第156行: 第158行:
el.setAttribute(startAttr, t1.toString());
el.setAttribute(startAttr, t1.toString());
el.setAttribute(endAttr, t2.toString());
el.setAttribute(endAttr, t2.toString());
handleElement(el);
}
}
function handleElement(el) {
const t1 = getDate(el.getAttribute(startAttr));
const t2 = getDate();
const cmd = el.getAttribute(cmdAttr) || "d";
const simple = el.classList.contains("simple");
const cpx = el.classList.contains("complex");
const t = timeDiff({ t1, t2, cmd, cpx, simple });
if (!cpx) {
if (el.textContent != t)
el.textContent = t;
} else {
if (el.innerHTML != t)
el.innerHTML = t;
}
}
}
}
第167行: 第185行:
const update = () => {
const update = () => {
for (let i = 0; i < elems.length; i++) {
for (let i = 0; i < elems.length; i++) {
setTimeout(() => {
handleElement(elems[i]);
const el = elems[i];
const t1 = getDate(el.getAttribute(startAttr));
const t2 = getDate();
const cmd = el.getAttribute(cmdAttr) || "d";
const cpx = el.classList.contains("complex");
const t = timeDiff({ t1, t2, cmd, cpx });
if (!cpx) {
if (el.textContent != t)
el.textContent = t;
} else {
if (el.innerHTML != t)
el.innerHTML = t;
}
}, 0);
}
}
};
};
第195行: 第199行:
});
});
})();
})();

</script></includeonly><noinclude>请使用模板{{tl|时间差}}。
</script></includeonly><noinclude>请使用模板{{tl|时间差}}。



2022年10月4日 (二) 19:02的最新版本

请使用模板{{时间差}}。

原微件搬运自我的世界玩家社区Wiki,原作者:Salt_lovely(原作者即搬运者)。