微件:TimeDiff:修订间差异

添加181字节 、​ 2022年10月4日 (星期二)
添加简化输出模式
(添加了年份、月份计算,支持 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);
       diff.day = days;
       if (!simple || days || "year" in diff || "month" in diff)
        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|时间差}}。


原微件搬运自[https://wiki.biligame.com/mcplayer/Widget:TimeDiff 我的世界玩家社区Wiki],原作者:Salt_lovely(原作者即搬运者)。</noinclude>
原微件搬运自[https://wiki.biligame.com/mcplayer/Widget:TimeDiff 我的世界玩家社区Wiki],原作者:Salt_lovely(原作者即搬运者)。</noinclude>