[Code] 格式化民國年
這個方法用 proxy 擴充 dayjs,讓 dayjs 支援格式化民國年。
JS 程式碼 // dayx.js import day from "dayjs"; const prototype = Object.getPrototypeOf(day()); const yyy = (dt) => (dt.getFullYear() - 1911).toString().padStart(3, "0"); const handler = { get: function (target, prop, receiver) { // age, dte, tme if (prop === "age") return day().diff(receiver, "years", false); if (prop === "dte") return receiver.format("YYYMMDD"); if (prop === "tme") return receiver.format("HHmmss"); // format if (prop === "format") return (format) => { const formattingTokens = /Y{4,}|Y{3}|[^Y{3}]+/g; const arr = format.