UNPKG

cleano

Version:

輕巧好用的工具庫,提供日期格式化與 HTML 字符轉義功能

54 lines (46 loc) 1.35 kB
function dateFormat(dateStr) { const dt = new Date(dateStr); const year = dt.getFullYear(); const month = padZero(dt.getMonth() + 1); const day = padZero(dt.getDate()); // ✅ 原本寫 getDay() 是星期幾,應該改成 getDate() const hh = padZero(dt.getHours()); const mm = padZero(dt.getMinutes()); const ss = padZero(dt.getSeconds()); return `${year}-${month}-${day} ${hh}:${mm}:${ss}`; } function padZero(n) { return n > 9 ? n : "0" + n; } function htmlEscape(htmlStr) { return htmlStr.replace(/[<>"'&]/g, (match) => { switch (match) { case "<": return "&lt;"; case ">": return "&gt;"; case '"': return "&quot;"; case "'": return "&#39;"; case "&": return "&amp;"; } }); } function htmlUnescape(str) { return str.replace(/&lt;|&gt;|&quot;|&#39;|&amp;/g, (match) => { switch (match) { case "&lt;": return "<"; case "&gt;": return ">"; case "&quot;": return '"'; case "&#39;": return "'"; case "&amp;": return "&"; } }); } export { dateFormat, htmlEscape, htmlUnescape };