Initial commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Date formatting and calculation utilities for Swedish locale (sv-SE).
|
||||
* All functions use ISO 8601 format for input (YYYY-MM-DD) and Intl API for display.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Format a date for display (e.g. "1 juni 2026" in Swedish).
|
||||
* Accepts ISO string or Date object.
|
||||
*/
|
||||
export function formatDateForDisplay(date: string | Date): string {
|
||||
const d = typeof date === 'string' ? new Date(date + 'T00:00:00Z') : date
|
||||
if (!isValidDate(d)) return ''
|
||||
const formatter = new Intl.DateTimeFormat('sv-SE', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
})
|
||||
return formatter.format(d)
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a date for HTML input value (e.g. "2026-06-01").
|
||||
* Accepts ISO string or Date object.
|
||||
*/
|
||||
export function formatDateForInput(date: string | Date): string {
|
||||
const d = typeof date === 'string' ? new Date(date + 'T00:00:00Z') : date
|
||||
if (!isValidDate(d)) return ''
|
||||
const year = d.getUTCFullYear()
|
||||
const month = String(d.getUTCMonth() + 1).padStart(2, '0')
|
||||
const day = String(d.getUTCDate()).padStart(2, '0')
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an ISO date string (YYYY-MM-DD) to a Date object.
|
||||
*/
|
||||
export function parseInputDate(value: string): Date {
|
||||
return new Date(value + 'T00:00:00Z')
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard: check if a value is a valid date.
|
||||
*/
|
||||
export function isValidDate(date: any): boolean {
|
||||
return date instanceof Date && !isNaN(date.getTime())
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Swedish day name (e.g. "Fredag").
|
||||
*/
|
||||
export function getDayName(date: Date): string {
|
||||
if (!isValidDate(date)) return ''
|
||||
const formatter = new Intl.DateTimeFormat('sv-SE', { weekday: 'long' })
|
||||
return formatter.format(date)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Swedish month name (e.g. "Juni").
|
||||
*/
|
||||
export function getMonthName(date: Date): string {
|
||||
if (!isValidDate(date)) return ''
|
||||
const formatter = new Intl.DateTimeFormat('sv-SE', { month: 'long' })
|
||||
return formatter.format(date)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Formatting utilities for Swedish locale (sv-SE).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Format a number of days with Swedish plural rules (1 dag, 2 dagar, etc.).
|
||||
*/
|
||||
export function formatDayCount(days: number): string {
|
||||
if (days === 1) return '1 dag'
|
||||
return `${days} dagar`
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a percentage from numerator and denominator (e.g. "75%").
|
||||
* Rounds to nearest integer.
|
||||
*/
|
||||
export function formatPercentage(numerator: number, denominator: number): string {
|
||||
if (denominator === 0) return '0%'
|
||||
const percentage = Math.round((numerator / denominator) * 100)
|
||||
return `${percentage}%`
|
||||
}
|
||||
Reference in New Issue
Block a user