import { readableDateDiff } from "./readableDateDiff" /** 格式化时间: 05月08日 */ export function MMDD(date = new Date()) { const month = (date.getMonth() + 1).toString().padStart(2, "0") const day = date.getDate().toString().padStart(2, "0") return `${month}月${day}日` } /** 格式化时间: 05月08日 08:20*/ export function MMDDHHMM(date = new Date()) { const month = (date.getMonth() + 1).toString().padStart(2, "0") const day = date.getDate().toString().padStart(2, "0") const hour = date.getHours().toString().padStart(2, "0") const minute = date.getMinutes().toString().padStart(2, "0") return `${month}月${day}日 ${hour}:${minute}` } /** 格式化时间: 2023/07/01 */ export function YYYYMMDD(date = new Date()) { const year = date.getFullYear() const month = (date.getMonth() + 1).toString().padStart(2, "0") const day = date.getDate().toString().padStart(2, "0") return `${year}/${month}/${day}` } /** 格式化时间: 2023/07/01 08:20 */ export function YYYYMMDDHHMM(date = new Date()) { const year = date.getFullYear() const month = (date.getMonth() + 1).toString().padStart(2, "0") const day = date.getDate().toString().padStart(2, "0") const hour = date.getHours().toString().padStart(2, "0") const minute = date.getMinutes().toString().padStart(2, "0") return `${year}/${month}/${day} ${hour}:${minute}` } /** 格式化时间: 2023/07/01 8:02 */ export function YYYYMMDDHMM(date = new Date()) { const year = date.getFullYear() const month = (date.getMonth() + 1).toString().padStart(2, "0") const day = date.getDate().toString().padStart(2, "0") const hour = date.getHours().toString() const minute = date.getMinutes().toString().padStart(2, "0") return `${year}/${month}/${day} ${hour}:${minute}` } /** 格式化时间: 2023/7/1 */ export function YYYYMD(date = new Date()) { const year = date.getFullYear() const month = (date.getMonth() + 1).toString() const day = date.getDate().toString() return `${year}/${month}/${day}` } /** 格式化时间: 2023/7/1 8:25 */ export function YYYYMDHM(date = new Date()) { const year = date.getFullYear() const month = (date.getMonth() + 1).toString() const day = date.getDate().toString() const hour = date.getHours().toString() const minute = date.getMinutes().toString() return `${year}/${month}/${day} ${hour}:${minute}` } /** 可读化日期 */ export function readableDate( date: string | Date | number = new Date(), options?: { /** 相当于 isShort:true isYYYYMMDDHMM:true */ auto?: boolean /** 把日期尽可能用「今天」、「昨天」、「前天」来替换,8 小时内的使用相对时间 */ isShort?: boolean /** 2023/07/01 */ isYYYYMMDD?: boolean /** 2023/7/1 */ isYYYYMD?: boolean /** 2023/07/01 08:02 */ isYYYYMMDDHHMM?: boolean /** 2023/07/01 8:02 */ isYYYYMMDDHMM?: boolean } ) { if (typeof date === "string") { date = new Date(date) } else if (typeof date === "number") { date = new Date(date) } if (date instanceof Date === false) { console.error("Invalid date:", date) return "unknown date" } let re: string if (options?.auto) { options.isShort = true options.isYYYYMMDDHMM = true } if (options?.isYYYYMMDD) { re = YYYYMMDD(date) } else if (options?.isYYYYMD) { re = YYYYMD(date) } else if (options?.isYYYYMMDDHHMM) { re = YYYYMMDDHHMM(date) } else if (options?.isYYYYMMDDHMM) { re = YYYYMMDDHMM(date) } else { re = YYYYMMDD(date) } if (options?.isShort) { // 把日期尽可能用「今天」、「昨天」来替换 const today = new Date() if (Math.abs(today.getTime() - date.getTime()) < 8 * 60 * 60 * 1000) { re = readableDateDiff(today, date, { ensureDate1Bigger: true, fuzzySecond: true }) } else { let dateDateString = date.toDateString() if (dateDateString === today.toDateString()) { re = "今天 " + re.split(" ").slice(1).join("") } else { const yesterday = new Date(today) yesterday.setDate(yesterday.getDate() - 1) if (dateDateString === yesterday.toDateString()) { re = "昨天 " + re.split(" ").slice(1).join("") } else { if (today.getFullYear() === date.getFullYear()) { re = MMDDHHMM(date) } } } } } return re } /** 可读化时间,相当于 readableDate(date, { auto: true }) * 日期尽可能用「今天」、「昨天」、「前天」来替换,8 小时内的使用相对时间 */ export function readableTime(date: string | Date | number = new Date()) { return readableDate(date, { auto: true }) }