Skip to content

Instantly share code, notes, and snippets.

@mhrstmnn
Created March 6, 2023 21:49
Show Gist options
  • Save mhrstmnn/428234fc8fa8b09a0f7a766d62c03f08 to your computer and use it in GitHub Desktop.
Save mhrstmnn/428234fc8fa8b09a0f7a766d62c03f08 to your computer and use it in GitHub Desktop.
function padNumber(n: number): string {
return n.toString().padStart(2, '0')
}
function formatDate(
date: Date,
offset?: Partial<{
year: number
month: number
day: number
hour: number
minute: number
}>
): string {
const year = date.getFullYear() + (offset?.year ?? 0),
month = padNumber(date.getMonth() + 1 + (offset?.month ?? 0)),
day = padNumber(date.getDate() + (offset?.day ?? 0)),
hour = padNumber(date.getHours() + (offset?.hour ?? 0)),
minute = padNumber(date.getMinutes() + (offset?.minute ?? 0))
return `${year}-${month}-${day}T${hour}:${minute}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment