|
@@ -4,13 +4,21 @@
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 格式化日期
|
|
* 格式化日期
|
|
|
- * @param date 日期字符串或Date对象
|
|
|
|
|
|
|
+ * @param date 日期字符串、时间戳或Date对象
|
|
|
* @param format 格式化模板,默认 'YYYY-MM-DD'
|
|
* @param format 格式化模板,默认 'YYYY-MM-DD'
|
|
|
*/
|
|
*/
|
|
|
-export const formatDate = (date: string | Date | null | undefined, format: string = 'YYYY-MM-DD'): string => {
|
|
|
|
|
|
|
+export const formatDate = (date: string | number | Date | null | undefined, format: string = 'YYYY-MM-DD'): string => {
|
|
|
if (!date) return '-'
|
|
if (!date) return '-'
|
|
|
|
|
|
|
|
- const d = typeof date === 'string' ? new Date(date) : date
|
|
|
|
|
|
|
+ let d: Date
|
|
|
|
|
+ if (typeof date === 'number') {
|
|
|
|
|
+ d = new Date(date)
|
|
|
|
|
+ } else if (typeof date === 'string') {
|
|
|
|
|
+ d = new Date(date)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ d = date
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (isNaN(d.getTime())) return '-'
|
|
if (isNaN(d.getTime())) return '-'
|
|
|
|
|
|
|
|
const year = d.getFullYear()
|
|
const year = d.getFullYear()
|
|
@@ -31,9 +39,9 @@ export const formatDate = (date: string | Date | null | undefined, format: strin
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 格式化日期时间
|
|
* 格式化日期时间
|
|
|
- * @param date 日期字符串或Date对象
|
|
|
|
|
|
|
+ * @param date 日期字符串、时间戳或Date对象
|
|
|
*/
|
|
*/
|
|
|
-export const formatDateTime = (date: string | Date | null | undefined): string => {
|
|
|
|
|
|
|
+export const formatDateTime = (date: string | number | Date | null | undefined): string => {
|
|
|
return formatDate(date, 'YYYY-MM-DD HH:mm:ss')
|
|
return formatDate(date, 'YYYY-MM-DD HH:mm:ss')
|
|
|
}
|
|
}
|
|
|
|
|
|