This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
frontend/src/helpers/time/isoToKebabDate.ts
Giacomo Rossetto 9b912af873
Some checks failed
continuous-integration/drone/pr Build is failing
Initial work
2023-10-03 22:18:25 +00:00

28 lines
879 B
TypeScript

import type {DateISO} from '@/types/DateISO'
import type {DateKebab, TimeKebab} from '@/types/DateKebab'
// ✅ Format a date to YYYY-MM-DD (or any other format)
function padTo2Digits(num: number) {
return num.toString().padStart(2, '0')
}
export function isoToKebabDate(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('-') as DateKebab
}
export function isoToKebabTime(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()), // January is 0, but we want it to be 1
padTo2Digits(date.getMinutes()),
].join(':') as TimeKebab
}