Refactor date formatting and add conditional dialog display

This commit is contained in:
Dede Fuji Abdul
2024-03-29 23:43:49 +07:00
parent ecd685227d
commit 3ca721e389
5 changed files with 134 additions and 37 deletions

View File

@ -62,4 +62,40 @@ const getMonthNumber = (monthName: string) => {
]
return months.indexOf(monthName) + 1
}
export { splitRoutePath, getMonthName, getMonthNameShort, getMonthNumber }
const reformatDate = (inputDate: string, originalFormat: string, targetFormat: string): string => {
// Parsing tanggal dari format asli
const originalDate = parseDate(inputDate, originalFormat)
// Format tanggal dalam format yang diinginkan
const formattedDate = formatDateToString(originalDate, targetFormat)
return formattedDate
}
const parseDate = (inputDate: string, format: string): Date => {
// Pisahkan tanggal, bulan, dan tahun dari input berdasarkan format
const parts = inputDate.split(/[^0-9]+/)
const formatParts = format.split(/[^a-zA-Z]+/)
// Buat objek tanggal baru dengan mengambil nilai dari format yang diberikan
const dateValues: any = {}
for (let i = 0; i < formatParts.length; i++) {
if (formatParts[i]) {
dateValues[formatParts[i]] = parseInt(parts[i], 10)
}
}
// Format tanggal dalam format yang diinginkan
return new Date(dateValues['yyyy'], dateValues['MM'] - 1, dateValues['dd'])
}
const formatDateToString = (date: Date, format: string): string => {
const day = date.getDate().toString().padStart(2, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const year = date.getFullYear().toString()
// Ganti placeholder dengan nilai tanggal, bulan, dan tahun
return format.replace('yyyy', year).replace('MM', month).replace('dd', day)
}
export { reformatDate, splitRoutePath, getMonthName, getMonthNameShort, getMonthNumber }