12432展示1.2万
122321121展示1.2亿
function formatCount(count) {
count = Number(count)
if (count >= 100000000) {
return (count / 100000000).toFixed(1) + '亿'
} else if (count >= 10000) {
return (count / 10000).toFixed(1) + '万'
} else {
return count
}
}
100秒展示01:40
6秒展示00:06
function padLeft(time) {
// if((time + '').length >= 2) return time
// return '0' + time
time = time + ''
return ('00' + time).slice(time.length)
}
function formatTime(time) {
let minute = Math.floor(time / 60) // 分
let second = Math.floor(time % 60) // 秒
return padLeft(minute) + ':' + padLeft(second)
}
根据时间戳格式化时间:yyyy-MM-dd hh:mm:ss
相关库:dayjs、moment
function formatDate(time, formatString) {
// 1.将时间戳转Date
const date = new Date(time)
// 2.获取时间值 定义正则和值之间的关系
const obj = {
'y+': date.getFullYear(), // 年
'M+': date.getMonth() + 1, // 月
'd+': date.getDate(), // 日
'h+': date.getHours(), // 时
'm+': date.getMinutes(), // 分
's+': date.getSeconds() // 秒
}
// 替换
for (const key in obj) {
const keysRe = new RegExp(key)
// 传入的字符串里是否有key
if (keysRe.test(formatString)) {
// padStart:有两位,用0补齐
const value = (obj[key] + '').padStart(2, '0')
formatString = formatString.replace(keysRe, value)
}
}
return formatString
}
const result = formatDate(1332322323, 'yyyy-MM-dd hh:mm:ss') // 1970-01-16 18:05:22