@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
139 lines (138 loc) • 6.52 kB
TypeScript
import { TZDate } from '@date-fns/tz';
declare global {
interface Window {
pktTz: string;
}
}
/**
* Returns the current date in the specified timezone, with time set to 00:00:00
* @param tz - Timezone string (default: window.pktTz)
* @returns Date object representing today's date in the specified timezone
*/
export declare const todayInTz: (tz?: string) => Date;
/**
* Parses an ISO date string and returns a Date object.
* Handles "YYYY-MM-DD", "YYYY-MM", and "YYYY" formats as local dates.
* @param date - The ISO date string to parse
* @returns A Date object
*/
export declare function parseISODateString(date: string | null | undefined): Date;
/**
* Formats a Date or TZDate object to an ISO date string (YYYY-MM-DD).
* Uses local time, not UTC.
* @param date - The Date or TZDate object to format
* @returns An ISO date string
*/
export declare const formatISODate: (date: Date | TZDate) => string;
/**
* Converts an ISO date string to a Date object
* @param date - The ISO date string to convert
* @returns A Date object or null if invalid
*/
export declare const fromISOToDate: (date: string | null) => Date | null;
/**
* Converts an ISO date string to a formatted date string
* @param date - The ISO date string to convert
* @param dateformat - The desired date format
* @returns A formatted date string
*/
export declare const fromISOtoLocal: (date: string, dateformat: string) => string;
/**
* Creates a new Date object based on the provided date string or Date object.
* Optionally sets the time to the start or end of the day.
* @param date - The date string or Date object
* @param timeOfDay - 'start' to set time to 00:00:00, 'end' to set time to 23:59:59
* @returns A Date object
*/
export declare const newDate: (date?: string | Date, timeOfDay?: "start" | "end") => Date;
/**
* Creates a new Date object based on the provided year, month, and day.
* If the inputs are invalid, returns the current date.
* @param year - The year
* @param month - The month (0-11)
* @param day - The day of the month (default: 1)
* @returns A Date object
*/
export declare const newDateYMD: (year: number, month: number, day?: number) => Date;
/**
* Creates a new Date object based on the provided date string or Date object.
* Sets the time to noon (12:00:00) to avoid timezone issues.
* @param date - The date string or Date object
* @returns A Date object with time set to noon
*/
export declare const newDateFromDate: (date: Date | TZDate | number) => Date;
export declare const formatReadableDate: (date: Date | TZDate) => string;
/**
* Validates if a date is within the specified range
* @param date - The date to validate
* @param min - The minimum date (ISO string)
* @param max - The maximum date (ISO string)
* @returns boolean indicating if the date is in range
*/
export declare const isDateInRange: (date: Date, min?: string | null, max?: string | null) => boolean;
/**
* Checks if a date is in the list of excluded dates
* @param date - The date to check
* @param excludedDates - Array of excluded date strings (ISO format)
* @returns boolean indicating if the date is excluded
*/
export declare const isDateExcluded: (date: Date, excludedDates: string[]) => boolean;
/**
* Checks if a date's weekday is excluded
* @param date - The date to check
* @param excludedWeekdays - Array of excluded weekdays (0 = Sunday, 1 = Monday, etc.)
* @returns boolean indicating if the weekday is excluded
*/
export declare const isWeekdayExcluded: (date: Date, excludedWeekdays: string[]) => boolean;
/**
* Validates if a date can be selected based on all constraints
* @param date - The date to validate
* @param min - The minimum date (ISO string)
* @param max - The maximum date (ISO string)
* @param excludedDates - Array of excluded date strings (ISO format)
* @param excludedWeekdays - Array of excluded weekdays (0 = Sunday, 1 = Monday, etc.)
* @returns boolean indicating if the date is selectable
*/
export declare const isDateSelectable: (date: Date, min?: string | null, max?: string | null, excludedDates?: string[], excludedWeekdays?: string[]) => boolean;
/**
* Filters an array of date strings to only include selectable dates
* @param dates - Array of date strings (ISO format)
* @param min - The minimum date (ISO string)
* @param max - The maximum date (ISO string)
* @param excludedDates - Array of excluded date strings (ISO format)
* @param excludedWeekdays - Array of excluded weekdays (0 = Sunday, 1 = Monday, etc.)
* @returns Array of selectable date strings
*/
export declare const filterSelectableDates: (dates: string[], min?: string | null, max?: string | null, excludedDates?: string[], excludedWeekdays?: string[]) => string[];
/**
* Sorts an array of ISO date strings chronologically
* @param dates - Array of date strings (ISO format)
* @returns Sorted array of date strings
*/
export declare const sortDateStrings: (dates: string[]) => string[];
/**
* Validates a date range (start should be before or equal to end)
* @param startDate - The start date (ISO string)
* @param endDate - The end date (ISO string)
* @returns boolean indicating if the date range is valid
*/
export declare const isValidDateRange: (startDate: string, endDate: string) => boolean;
declare const _default: {
todayInTz: (tz?: string) => Date;
parseISODateString: typeof parseISODateString;
formatISODate: (date: Date | TZDate) => string;
fromISOToDate: (date: string | null) => Date | null;
fromISOtoLocal: (date: string, dateformat: string) => string;
newDate: (date?: string | Date, timeOfDay?: "start" | "end") => Date;
newDateYMD: (year: number, month: number, day?: number) => Date;
newDateFromDate: (date: Date | TZDate | number) => Date;
formatReadableDate: (date: Date | TZDate) => string;
isDateInRange: (date: Date, min?: string | null, max?: string | null) => boolean;
isDateExcluded: (date: Date, excludedDates: string[]) => boolean;
isWeekdayExcluded: (date: Date, excludedWeekdays: string[]) => boolean;
isDateSelectable: (date: Date, min?: string | null, max?: string | null, excludedDates?: string[], excludedWeekdays?: string[]) => boolean;
filterSelectableDates: (dates: string[], min?: string | null, max?: string | null, excludedDates?: string[], excludedWeekdays?: string[]) => string[];
sortDateStrings: (dates: string[]) => string[];
isValidDateRange: (startDate: string, endDate: string) => boolean;
};
export default _default;