没毛病清清爽爽,没毛病清清爽爽,没毛病清清爽爽,没毛病清清爽爽,
没毛病清清爽爽,没毛病清清爽爽,没毛病清清爽爽,没毛病清清爽爽,
<script type="text/javascript">
// 填写JavaScript
const formatDate = (date, template) => {
const week = ["日", "一", '二', '三', '四', '五', '六'];
const yyyy = date.getFullYear(); // 年
const yy = yyyy % 100; // 年份(取尾数)
const M = date.getMonth() + 1; // 月
const MM = M > 9 ? M : `0${M}`; // 月(补0)
const d = date.getDate(); // 日
const dd = d > 9 ? d : `0${d}` // 日(补0)
const H = date.getHours(); // 24时制
const HH = H > 9 ? H : `0${H}`; // 24时制(补0)
const h = H > 12 ? (H % 12) : H; // 12时制
const hh = h > 9 ? h : `0${h}`; // 12时制(补0)
const m = date.getMinutes(); // 分
const mm = m > 9 ? m : `0${m}`; // 分(补0)
const s = date.getSeconds(); // 秒
const ss = s > 9 ? s : `0${s}`; // 秒(补0)
const w = week[date.getDay()]; // 周几
// 创建对象方便后续正则匹配映射值
const formatObj = {yyyy,yy,M,MM,d,dd,H,HH,h,hh,m,mm,s,ss,w}
// \b边界匹配防止yy匹配到yyyy上面去
template = template.replace(/\b(yyyy|yy|M|MM|d|dd|H|HH|h|hh|m|mm|s|ss|w)\b/g, (match) => {
return `${formatObj[match]}`
})
/*
// 循环遍历对象替换的方式【可选】
for(const key in formatObj){
let reg = new RegExp(`\\b${key}\\b`, "g");
template = template.replace(reg, formatObj[key])
}
*/
return template;
}
</script>



京公网安备 11010502036488号