cleano
Version:
輕巧好用的工具庫,提供日期格式化與 HTML 字符轉義功能
54 lines (46 loc) • 1.35 kB
JavaScript
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 "<";
case ">":
return ">";
case '"':
return """;
case "'":
return "'";
case "&":
return "&";
}
});
}
function htmlUnescape(str) {
return str.replace(/<|>|"|'|&/g, (match) => {
switch (match) {
case "<":
return "<";
case ">":
return ">";
case """:
return '"';
case "'":
return "'";
case "&":
return "&";
}
});
}
export { dateFormat, htmlEscape, htmlUnescape };