john_lee
Version:
DateFormat
41 lines (35 loc) • 1.04 kB
JavaScript
function dataFormat(dtStr) {
const dt = new Date(dtStr)
//定义常量接收js内置时间函数
const y = dt.getFullYear()
//因为月从0开始,所以需要手动+1
const m = padZero(dt.getMonth() + 1)
const d = padZero(dt.getDate())
const hh = padZero(dt.getHours())
const mm = padZero(dt.getMinutes())
const ss = padZero(dt.getSeconds())
//return `yyyy-mm-dd hh:mm:ss`
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
//补零操作
function padZero(n) {
return n > 9 ? n : '0' + n
}
//转义HTML字符的函数
function htmlEscape(htmlstr) {
// "//g"表示//之间的全匹配
return htmlstr.replace(/<|>|"|&/g, match => {
switch (match) {
case '<':
return '<'
case '>':
return '>'
case '"':
return '"'
case '&':
return '&'
}
})
}
//共享对象
module.exports = { dataFormat, htmlEscape }