140 lines
3.9 KiB
TypeScript
140 lines
3.9 KiB
TypeScript
import dayjs from 'dayjs'
|
||
|
||
// 定义单位的类型
|
||
type Unit = 'day' | 'week' | 'month' | 'year'
|
||
|
||
/**
|
||
* 格式化时间字符串为 yyyy-MM-DD HH:mm:ss
|
||
* @param dateStr 原始时间字符串,例如 "2025-12-04 00:00:00.0"
|
||
*/
|
||
export function formatDateTime(dateStr: string): string {
|
||
if (!dateStr)
|
||
return ''
|
||
return dayjs(dateStr).format('YYYY-MM-DD HH:mm:ss')
|
||
}
|
||
/**
|
||
* 获取指定日期之前的日期
|
||
* @param value 数字,表示要减去的时间量
|
||
* @param unit 时间单位,可以是 'day'、'week'、'month' 或 'year'
|
||
* @returns 返回计算后的日期
|
||
*/
|
||
export function getDateBefore(value: number, unit: Unit): string {
|
||
return dayjs().subtract(value, unit).format('YYYY-MM-DD')
|
||
}
|
||
// 获取日期的差额
|
||
export function getTimeDifference(dateStr1: string, dateStr2: string): number {
|
||
if (!dateStr1 || !dateStr2)
|
||
return 0
|
||
|
||
const date1 = new Date(dateStr1)
|
||
const date2 = new Date(dateStr2)
|
||
|
||
if (Number.isNaN(date1.getTime()) || Number.isNaN(date2.getTime()))
|
||
return 0
|
||
|
||
const diffMs = Math.abs(date2.getTime() - date1.getTime())
|
||
const diffDays = diffMs / (1000 * 60 * 60 * 24)
|
||
|
||
return Math.floor(diffDays)
|
||
}
|
||
|
||
// 获取当前年份
|
||
export function getYear(): string {
|
||
return dayjs().year().toString()
|
||
}
|
||
export function getMonth(): string {
|
||
// 获取当前月份并加1,确保是1-12范围
|
||
const month = (dayjs().month() + 1).toString()
|
||
return month.padStart(2, '0') // 如果是单数月,前面加0
|
||
}
|
||
|
||
export function getDay(): string {
|
||
// 获取当前日期并转换为字符串
|
||
const day = dayjs().date().toString()
|
||
return day.padStart(2, '0') // 如果是单数天,前面加0
|
||
}
|
||
|
||
export function getCurrentDate() {
|
||
return dayjs().format('YYYY-MM-DD')
|
||
}
|
||
|
||
export function getCurrentDateTimeWithSeconds() {
|
||
return dayjs().format('YYYY-MM-DD HH:mm:ss')
|
||
}
|
||
|
||
export function stringToTimestamp(timeString: string, format: string): number {
|
||
const parsedDate = dayjs(timeString, format)
|
||
if (!parsedDate.isValid()) {
|
||
throw new Error('Invalid date string or format')
|
||
}
|
||
return parsedDate.valueOf() // 返回时间戳(以毫秒为单位)
|
||
}
|
||
|
||
export function timestampToString(timestamp: number, format: string): string {
|
||
const parsedDate = dayjs(timestamp)
|
||
return parsedDate.format(format)
|
||
}
|
||
|
||
/**
|
||
* 判断传入日期是否在今天的60天以内(含60天)
|
||
* @param dateStr - 日期字符串(如 '2025-02-01')
|
||
* @returns boolean - true 表示在60天内(包括第60天),false 表示超过60天
|
||
*/
|
||
export function isWithin60Days(dateStr: string): boolean {
|
||
if (!dateStr.trim()) {
|
||
return true
|
||
}
|
||
const inputDate = dayjs(dateStr)
|
||
const today = dayjs()
|
||
const diffDays = today.diff(inputDate, 'day')
|
||
return diffDays >= 0 && diffDays <= 60
|
||
}
|
||
|
||
// 获取到两个日期之间的差值
|
||
export function getDateDiff(startDate, endDate) {
|
||
// 任意一个为空,返回 0
|
||
if (!startDate || !endDate)
|
||
return 0
|
||
|
||
try {
|
||
const start = new Date(startDate)
|
||
const end = new Date(endDate)
|
||
|
||
// 如果日期无效,返回 0
|
||
if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime()))
|
||
return 0
|
||
|
||
const diffTime = Math.abs(end.getTime() - start.getTime())
|
||
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24))
|
||
|
||
return diffDays
|
||
}
|
||
catch {
|
||
return 0
|
||
}
|
||
}
|
||
export function formatDatetimeToDate(date) {
|
||
if (!date) {
|
||
return '' // 如果传入的日期为空,返回空字符串
|
||
}
|
||
return dayjs(date).format('YYYY-MM-DD') // 格式化为 年-月-日
|
||
}
|
||
|
||
export function formatTimestampToDate(timestamp: string | number) {
|
||
const date = new Date(timestamp)
|
||
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}`
|
||
}
|
||
|
||
export function formatTimestampToDateTime(
|
||
timestamp: string | number,
|
||
format = 'YYYY-MM-DD HH:mm:ss',
|
||
): string {
|
||
if (!timestamp)
|
||
return ''
|
||
|
||
return dayjs(timestamp).format(format)
|
||
}
|