feat: Add isoToKebabDatetime helper

This commit is contained in:
Wake Liu 2024-04-27 02:00:51 +08:00
parent 9e2a6d5575
commit d183fafa1e
1 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1,19 @@
import type {DateISO} from '@/types/DateISO'
import type {DatetimeKebab} from '@/types/DatetimeKebab'
// ✅ Format a date to YYYY-MM-DD HH:MM
function padTo2Digits(num: number) {
return num.toString().padStart(2, '0')
}
export function isoToKebabDatetime(isoDate: DateISO) {
const date = new Date(isoDate)
return [
date.getFullYear(),
padTo2Digits(date.getMonth() + 1), // January is 0, but we want it to be 1
padTo2Digits(date.getDate()),
].join('-') + ' ' + [
padTo2Digits(date.getHours()),
padTo2Digits(date.getMinutes()),
].join(':') as DatetimeKebab
}