1.年月日
export const formatDate = (value: string | number | null | undefined) => {?
? if (!value) {? ? ? ?
return undefined? ?
}? ?
if (isNumber(value)) {?
const date = value.toString()? ? ? ?
const year = date.substring(0, 4)? ? ? ?
const month = date.substring(4, 6)? ? ? ?
const day = date.substring(6, 8)? ? ? ?
return `${year}-${month}-${day}`? ?
}? ?
const date = new Date(value)? ?
const year = date.getFullYear()? ?
const month = fillZero(date.getMonth() + 1)? ?
const day = fillZero(date.getDate())? ?
return `${year}-${month}-${day}`
}
2.年月日時(shí)分秒
export const formatDateTime = (value: string | Date) => {
? ? const date = value instanceof Date ? value : new Date(value)
? ? const year = date.getFullYear()
? ? const month = date.getMonth() + 1
? ? const day = date.getDate()
? ? const hours = date.getHours()
? ? const minutes = date.getMinutes()
? ? const seconds = date.getSeconds()
? ? return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}