LLWiki正在建设中,欢迎加入我们

Widget:Countdown

来自LLWiki
Bhsd讨论 | 贡献2020年8月18日 (二) 03:58的版本 (创建页面,内容为“<noinclude>{{doc|content=引自萌娘百科:Widget:Countdown。 请使用{{tl|countdown}}调用。}}</noinclude><includeonly><!--{if !i…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转到导航 跳转到搜索
Template-info.png 小部件文档
这个文档是内联文档。

引自萌娘百科:Widget:Countdown

请使用{{countdown}}调用。

Javascript脚本:

"use strict";
window.RLQ = window.RLQ || [];
window.RLQ.push(async () => {
    await mw.loader.using("moment");
    const fromNow = function (then, before, after) {
        const now = moment();
        const isBefore = then.isBefore(now);
        const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11]; // 月份从0开始
        let year = isBefore ? now.year() - then.year() : then.year() - now.year(),
            month = isBefore ? now.month() - then.month() : then.month() - now.month(),
            day = isBefore ? now.date() - then.date() : then.date() - now.date(),
            hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
            minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
            second = isBefore ? now.second() - then.second() : then.second() - now.second();
        if (second < 0) {
            minute--;
            second += 60;
        }
        if (minute < 0) {
            hour--;
            minute += 60;
        }
        if (hour < 0) {
            day--;
            hour += 24;
        }
        if (day < 0) {
            month--;
            if (monthsHave31Days.includes((isBefore ? then : now).month())) {
                day += 31;
            } else if ((isBefore ? then : now).month() === 1) {
                if ((isBefore ? then : now).year() % 4 === 0) {
                    day += 29;
                } else {
                    day += 28;
                }
            } else {
                day += 30;
            }
        }
        if (month < 0) {
            year--;
            month += 12;
        }
        let result = "";
        if (year > 0) {
            result += `${year}年`;
        }
        if (month > 0) {
            result += `${month}月`;
        } else if (result !== "") {
            result += `${0}月`;
        }
        if (day > 0) {
            result += `${day}日`;
        } else if (result !== "") {
            result += `${0}日`;
        }
        if (hour > 0) {
            result += `${hour}小时`;
        } else if (result !== "") {
            result += `${0}小时`;
        }
        if (minute > 0) {
            result += `${minute}分`;
        } else if (result !== "") {
            result += `${0}分`;
        }
        if (second > 0) {
            result += `${second}秒`;
        } else if (result !== "") {
            result += `${0}秒`;
        }
        return (isBefore ? before : after).replace("$1", result.replace(/(\d) /g, "$1"));
    };
    const run = () => {
        $(".countdownNode:not(.disabled)").each((_, ele) => {
            const self = $(ele);
            self.text(fromNow(self.data("target"), ele.dataset.before || "$1前", ele.dataset.after || "还剩$1"));
        });
    };
    $(".countdownNode").each((_, ele) => {
        const self = $(ele),
            time = moment(ele.dataset.target);
        if (!time || !time.isValid()) {
            self.addClass("error disabled").text("(发生了致命错误!)");
            return;
        }
        self.data("target", time);
    });
    run();
    window.setInterval(run, 1000);
});
//