Commit 5a987d7c authored by qinhaitao's avatar qinhaitao

feat: 🎸 时间格式化

parent 5ed33856
/** @format */
import * as dayjs from 'dayjs'
import { getEndTimestamp, getStartTimestamp, transformBeijingDate } from '../../sdk'
const EIGHT_HOURS = 8 * 60 * 60 * 1000
/**
* 格式化时间 统一处理8小时时差问题
* YYYY-MM-DDTHH:mm:ss
*
* formatDate().format('YYYY-MM-DD HH:mm:ss')
* formatDate('2021/01/21').getStartTimestamp()
* formatDate('2021/01/21').getEndTimestamp()
* formatDate('2021/01/21').getTimestamp()
* @export
* @param {(string | number)} date
* @return {
* format 格式化时间
* getTimestamp 时间戳
* getStartTimestamp 某天开始的时间戳
* getEndTimestamp 某天结束的时间戳
* }
*/
export function formatDate(date: string | number = Date.now()) {
const beijingDate = typeof date === 'string' ? new Date(date) : transformBeijingDate(date)
return {
format: (template = 'YYYY-MM-DD') => dayjs(beijingDate).format(template),
getTimestamp: () => (typeof date === 'number' ? date : dayjs(date).valueOf() - EIGHT_HOURS),
getStartTimestamp: () => getStartTimestamp(dayjs(beijingDate).format('YYYY/MM/DD')),
getEndTimestamp: () => getEndTimestamp(dayjs(beijingDate).format('YYYY/MM/DD'))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment