!391 perf: utils/formatDate.ts更好的ts类型

* perf: utils/formatDate.ts更好的ts类型

(cherry picked from commit 9e86e47c1c)
This commit is contained in:
dhb52 2024-02-27 12:36:49 +00:00 committed by shizhong
parent 49f169b7ba
commit 1a9c87ffe7

View File

@ -1,4 +1,5 @@
import dayjs from 'dayjs'
import type { TableColumnCtx } from 'element-plus'
/**
* el-date-picker
@ -68,10 +69,7 @@ export function formatDate(date: Date, format?: string): string {
return ''
}
// 日期存在,则进行格式化
if (format === undefined) {
format = 'YYYY-MM-DD HH:mm:ss'
}
return dayjs(date).format(format)
return date ? dayjs(date).format(format ?? 'YYYY-MM-DD HH:mm:ss') : ''
}
// TODO 芋艿:稍后去掉
@ -218,7 +216,7 @@ export function formatAxis(param: Date): string {
* @param ms
* @returns {string}
*/
export function formatPast2(ms) {
export function formatPast2(ms: number): string {
const day = Math.floor(ms / (24 * 60 * 60 * 1000))
const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
@ -246,12 +244,8 @@ export function formatPast2(ms) {
* @param column
* @param cellValue
*/
// @ts-ignore
export const dateFormatter = (row, column, cellValue): string => {
if (!cellValue) {
return ''
}
return formatDate(cellValue)
export function dateFormatter(_row: any, _column: TableColumnCtx<any>, cellValue: any): string {
return cellValue ? formatDate(cellValue) : ''
}
/**
@ -261,12 +255,8 @@ export const dateFormatter = (row, column, cellValue): string => {
* @param column
* @param cellValue
*/
// @ts-ignore
export const dateFormatter2 = (row, column, cellValue) => {
if (!cellValue) {
return
}
return formatDate(cellValue, 'YYYY-MM-DD')
export function dateFormatter2(_row: any, _column: TableColumnCtx<any>, cellValue: any): string {
return cellValue ? formatDate(cellValue, 'YYYY-MM-DD') : ''
}
/**
@ -274,7 +264,7 @@ export const dateFormatter2 = (row, column, cellValue) => {
* @param param
* @returns 带时间00:00:00
*/
export function beginOfDay(param: Date) {
export function beginOfDay(param: Date): Date {
return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 0, 0, 0)
}
@ -283,7 +273,7 @@ export function beginOfDay(param: Date) {
* @param param
* @returns 带时间23:59:59
*/
export function endOfDay(param: Date) {
export function endOfDay(param: Date): Date {
return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 23, 59, 59)
}
@ -292,7 +282,7 @@ export function endOfDay(param: Date) {
* @param param1 1
* @param param2 2
*/
export function betweenDay(param1: Date, param2: Date) {
export function betweenDay(param1: Date, param2: Date): number {
param1 = convertDate(param1)
param2 = convertDate(param2)
// 计算差值
@ -304,7 +294,7 @@ export function betweenDay(param1: Date, param2: Date) {
* @param param1
* @param param2
*/
export function addTime(param1: Date, param2: number) {
export function addTime(param1: Date, param2: number): Date {
param1 = convertDate(param1)
return new Date(param1.getTime() + param2)
}
@ -313,7 +303,7 @@ export function addTime(param1: Date, param2: number) {
*
* @param param
*/
export function convertDate(param: Date | string) {
export function convertDate(param: Date | string): Date {
if (typeof param === 'string') {
return new Date(param)
}