Refactor dateTimeFormat (#569)

# PR Checklist
- [ ] Did you check if it works normally in all models? *ignore this
when it dosen't uses models*
- [ ] Did you check if it works normally in all of web, local and node
hosted versions? if it dosen't, did you blocked it in those versions?
- [ ] Did you added a type def?

# Description
It appears that the current date time formatting in CBS is not working
as intended, I also added a format for the day of the week (`ddd` and
`dddd`), please review if this is appropriate.
This commit is contained in:
kwaroran
2024-07-15 10:17:35 +09:00
committed by GitHub

View File

@@ -1496,17 +1496,20 @@ const dateTimeFormat = (main:string, time = 0) => {
return main
.replace(/YYYY/g, date.getFullYear().toString())
.replace(/YY/g, date.getFullYear().toString().substring(2))
.replace(/MM?/g, (date.getMonth() + 1).toString().padStart(2, '0'))
.replace(/DD?/g, date.getDate().toString().padStart(2, '0'))
.replace(/DDDD?/g, (date.getDay() + (date.getMonth() * 30)).toString())
.replace(/HH?/g, date.getHours().toString().padStart(2, '0'))
.replace(/hh?/g, (date.getHours() % 12).toString().padStart(2, '0'))
.replace(/mm?/g, date.getMinutes().toString().padStart(2, '0'))
.replace(/ss?/g, date.getSeconds().toString().padStart(2, '0'))
.replace(/X?/g, (Date.now() / 1000).toFixed(2))
.replace(/x?/g, Date.now().toFixed())
.replace(/MMMM/g, Intl.DateTimeFormat('en', { month: 'long' }).format(date))
.replace(/MMM/g, Intl.DateTimeFormat('en', { month: 'short' }).format(date))
.replace(/MM/g, (date.getMonth() + 1).toString().padStart(2, '0'))
.replace(/DDDD/g, Math.floor((date.getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / (1000 * 60 * 60 * 24)).toString())
.replace(/DD/g, date.getDate().toString().padStart(2, '0'))
.replace(/dddd/g, Intl.DateTimeFormat('en', { weekday: 'long' }).format(date))
.replace(/ddd/g, Intl.DateTimeFormat('en', { weekday: 'short' }).format(date))
.replace(/HH/g, date.getHours().toString().padStart(2, '0'))
.replace(/hh/g, (date.getHours() % 12 || 12).toString().padStart(2, '0'))
.replace(/mm/g, date.getMinutes().toString().padStart(2, '0'))
.replace(/ss/g, date.getSeconds().toString().padStart(2, '0'))
.replace(/X/g, Math.floor(date.getTime() / 1000).toString())
.replace(/x/g, date.getTime().toString())
.replace(/A/g, date.getHours() >= 12 ? 'PM' : 'AM')
.replace(/MMMM?/g, Intl.DateTimeFormat('en', { month: 'long' }).format(date))
}