✨(frontend) add date formatting utility function
Create reusable utility function to format dates into specified patterns. Provides consistent date formatting across the application.
This commit is contained in:
committed by
aleb_the_flash
parent
a8053b46cd
commit
67967f00b5
29
src/frontend/src/utils/formatDate.ts
Normal file
29
src/frontend/src/utils/formatDate.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
export function formatDate(
|
||||||
|
date: Date | string | number,
|
||||||
|
format: string = 'YYYY-MM-DD'
|
||||||
|
): string {
|
||||||
|
const dateObj = date instanceof Date ? date : new Date(date)
|
||||||
|
|
||||||
|
if (isNaN(dateObj.getTime())) {
|
||||||
|
return 'Invalid Date'
|
||||||
|
}
|
||||||
|
|
||||||
|
const year = dateObj.getFullYear()
|
||||||
|
const month = dateObj.getMonth() + 1 // getMonth() returns 0-11
|
||||||
|
const day = dateObj.getDate()
|
||||||
|
const hours = dateObj.getHours()
|
||||||
|
const minutes = dateObj.getMinutes()
|
||||||
|
const seconds = dateObj.getSeconds()
|
||||||
|
|
||||||
|
const pad = (num: number): string => String(num).padStart(2, '0')
|
||||||
|
|
||||||
|
let result = format
|
||||||
|
result = result.replace(/YYYY/g, year.toString())
|
||||||
|
result = result.replace(/MM/g, pad(month))
|
||||||
|
result = result.replace(/DD/g, pad(day))
|
||||||
|
result = result.replace(/HH/g, pad(hours))
|
||||||
|
result = result.replace(/mm/g, pad(minutes))
|
||||||
|
result = result.replace(/ss/g, pad(seconds))
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user