export const parseTime = function (time, cFormat) {
if (arguments.length === 0 || !time) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date = null
if (typeof time === 'object') {
date = time
} else {
if (typeof time === 'string') {
date = new Date(time)
} else {
if (typeof time === 'number' && `${time}`.length === 10) {
time = parseInt(time) * 1000
}
date = new Date(time)
}
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') return ['日', '一', '二', '三', '四', '五', '六'][value - 0]
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return timeStr
}
/*
desc: 从周一/日开始 默认周一
weekStr:数据前的字符 默认周
num: 返回的个数 默认7个
*/
export const toolsWeeksCN = (weekStr = '周', num = 7, desc = true) => {
const endStr = weekStr === '周' ? '日' : '天'
const weeks = [`${weekStr}一`, `${weekStr}二`, `${weekStr}三`, `${weekStr}四`, `${weekStr}五`, `${weekStr}六`, `${weekStr}${endStr}`]
if (!desc) {
const newArr = [weeks[weeks.length - 1], ...weeks.slice(0, weeks.length - 1)]
return [...newArr].slice(0, num)
}
return weeks.slice(0, num)
}
使用第一个参数是时间对象,,第二个参数就是需要返回的格式 可以自行定义 ' {y}/{m}/{d} {h}:{i}:{s} '