1.0.0 • Published 2 years ago

luox-tools v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

提供了基本的格式化时间的方法

运用dateFormat()函数进行时间的格式化

提供html和字符串之间的互相转义

使用htmlEscape()和htmlUnEscape()函数进行转义

实例:

// 定义一个格式化时间

function dateFormat(dtStr) { // 初始化时间 const dt = new Date(dtStr)

const y = dt.getFullYear()
const m = padZero(dt.getMonth() + 1)
const d = dt.getDay()

const hh = dt.getHours()
const mm = padZero(dt.getMinutes())
const ss = padZero(dt.getSeconds())

// 返回完整格式的时间
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`

}

// 定义补零的函数 function padZero(n) { // 三目运算 return n > 9 ? n: '0' + n }

// 定义转义HTML的方法 function htmlEscape(htmlstr){ return htmlstr.replace(/<|>|"|&/g,match => { switch(match){ case '<': return '<' case '>': return '>' case '"': return '"' case '&': return '&'
} }) }

// 定义转为HTML的方法 function htmlUnEscape(htmlStr){ return htmlStr.replace(/<|>|"|&/g,match => { switch(match){ case '<': return '<' case '>': return '>' case '"': return ':' case '&': return '&'
} }) }

// 向外暴露需要的成员 module.exports = { dateFormat, htmlEscape, htmlUnEscape }