caonpm-tools
Version:
提供了格式化时间,HTMLescape等相关功能
55 lines • 1.43 kB
JavaScript
// 包的入口文件
// 格式化时间的方法
function formattime(datestr) {
const tm = new Date(datestr)
const yy = padzero(tm.getFullYear())
const mm = padzero(tm.getMonth() + 1)
const dd = padzero(tm.getDay())
const hh = padzero(tm.getHours())
const MM = padzero(tm.getMinutes())
const ss = padzero(tm.getSeconds())
return `${yy}-${mm}-${dd} ${hh}:${MM}:${ss}`
}
// 补零函数
function padzero(padzerostr) {
return padzerostr > 9 ? padzerostr : '0' + padzerostr
}
// html转义函数
function turnHTML(htmlSTR) {
return htmlSTR.replace(/<|>|"|&/g, (value) => {
switch (value) {
case "<":
return "lt"
case ">":
return "gt"
case '"':
return """
case "&":
return "&"
default:
break;
}
})
}
// 转义html恢复函数
function restore(htmlSTR){
return htmlSTR.replace(/lt|gt|"|&/g, (value) => {
switch (value) {
case "lt":
return "<"
case "gt":
return ">"
case """:
return '"'
case "&":
return "&"
default:
break;
}
})
}
module.exports = {
formattime,
turnHTML,
restore
}