“Widget:Countdown”的版本间差异

添加22字节 、​ 2020年10月30日 (五) 05:47
无编辑摘要
(撤销Bhsd讨论)的版本11063)
标签撤销
<noinclude>{{doc|content={{CCnotice|MGP=widget:countdown}}
 
请使用{{tl|countdown}}调用。}}</noinclude><includeonly><!--{if !isset($wgCountdown) || !$wgCountdown}--><!--{assign var="wgCountdown" value=true scope="global"}--><script src="/mediawiki/resources/lib/moment/moment.js"></script><script>
//</includeonly>JavaScript脚本:<pre lang=js>
"use strict";
window.addEventListener('load', function () {
const fromNow = function (ele) {
let now = momentnew Date(),
then = momentnew Date(ele.dataset.target),
before = ele.children[0],
after = ele.children[1],
i18n = ele.dataset.title,
isBefore = then.isBefore(then < now),
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
year = isBefore ? now.yeargetFullYear() - then.yeargetFullYear() : then.yeargetFullYear() - now.yeargetFullYear(),
month = isBefore ? now.monthgetMonth() - then.monthgetMonth() : then.monthgetMonth() - now.monthgetMonth(),
day = isBefore ? now.dategetDate() - then.dategetDate() : then.dategetDate() - now.dategetDate(),
hour = isBefore ? now.hourgetHours() - then.hourgetHours() : then.hourgetHours() - now.hourgetHours(),
minute = isBefore ? now.minutegetMinutes() - then.minutegetMinutes() : then.minutegetMinutes() - now.minutegetMinutes(),
second = isBefore ? now.secondgetSeconds() - then.secondgetSeconds() : then.secondgetSeconds() - now.secondgetSeconds(),
result = "";
if (second < 0) {
if (day < 0) {
month--;
if (monthsHave31Days.includes( (isBefore ? then : now).monthgetMonth() )) { day += 31; }
else if ((isBefore ? then : now).monthgetMonth() === 1) {
if ((isBefore ? then : now).yeargetFullYear() % 4 === 0) { day += 29; }
else { day += 28; }
}
month += 12;
}
if (year > 0) { result += `<span class="countdown-num">${year}</` + '${"span"}>年'`; }
if (month > 0) { result += `<span class="countdown-num">${month}</` + '${"span"}><span class="countdown-month"><' + '/${"span"}>'`; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + '${"span"}><span class="countdown-month"><' + '/${"span"}>'`; }
if (day > 0) { result += `<span class="countdown-num">${day}</` + '${"span"}>天'`; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + '${"span"}>天'`; }
if(year == 0 && month == 0) {
if (hour > 0) { result += `<span class="countdown-num">${hour}</` + '${"span"}><span class="countdown-hour"><' + '/${"span"}>'`; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + '${"span"}><span class="countdown-hour"><' + '/${"span"}>'`; }
if(day == 0) {
if (minute > 0) { result += `<span class="countdown-num">${minute}</` + '${"span"}>分'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + '${"span"}>分'; }
if (second > 0) { result += `<span class="countdown-num">${second}</` + '${"span"}>秒'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + '${"span"}>秒'; }
}
}
};
document.querySelectorAll( '.countdownNode' ).forEach((ele) => {
const time = momentnew Date(ele.dataset.target);
if (!time || !time.isValidgetTime()) {
ele.classList.add("error", "disabled")
ele.textContent = "(时间格式错误!)";
16,874

个编辑