/**
* 获取本周
*/
export function WeekDay(date) {
const Nowdate = date ? new Date(date) : new Date()
const weekDay = Nowdate.getDay()
let start = Nowdate.getTime()
start = start - 1000 * 60 * 60 * 24 * (weekDay - 1)
const end = start + 1000 * 60 * 60 * 24 * 6
const startDate = new Date(start)
const endDate = new Date(end)
return `${getDate(startDate, 'Y-M-D')} - ${getDate(endDate, 'Y-M-D')}`
}
/**
*获取日期格式
*/
export function getDate(date, format) {
if (!isDate(date) && typeof date !== 'object') {
return ''
}
let obj = {}
if (typeof date === 'object' && date.toString() !== '[object Object]') {
const [Y, M, D, h, m, s] = [date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()]
obj = { Y, M, D, h, m, s }
} else {
const [t1, t2] = date.split(' ')
const [Y, M, D] = t1.split('-')
const [h, m, s] = t2 ? t2.split(':') : '0:0:0'
obj = { Y, M, D, h, m, s }
}
const reg = /Y|M|D|h|m|s/g
return format.replace(reg, rs => obj[rs].toString().length < 2 ? '0' + obj[rs] : obj[rs])
}
/**
* 是否日期
*/
export function isDate(str) {
return /^\d{4}(-\d{1,2}){2}( \d{1,2}(:\d{1,2}){2})?$/.test(str)
}
网友评论