UNPKG

@akala/core

Version:
57 lines (56 loc) 1.84 kB
import type { Formatter, ReversibleFormatter } from "./common.js"; export interface DateFormatterSettings { format: string; } /** * Parses and formats dates according to the specified format string. * * @param format - The date format string (e.g., 'yyyy-MM-dd'). * @returns An object with format and parse functions for date conversion. */ export declare function formatParser(format: string, locale: string | Intl.Locale): { /** * Formats a Date object into a string using the configured format. * * @param value - The Date to format. * @returns Formatted date string. */ format(value: Date): string; /** * Parses a date string into a Date object using the configured format. * * @param value - The date string to parse. * @returns Parsed Date object. * @throws {Error} If the input doesn't match the format. */ parse(value: string): Date; }; /** * Formatter for converting dates to strings and vice versa. */ export default class DateFormatter implements Formatter<string>, ReversibleFormatter<string, Date> { readonly dateFormat: ReturnType<typeof formatParser>; /** * Creates a date formatter with the specified format. * * @param dateFormat - The format string (e.g., 'yyyy-MM-dd'). */ constructor(dateFormat: string | { format: string; locale: string | Intl.Locale; }); /** * Parses a formatted date string back into a Date object. * * @param {string} value - The date string to parse. * @returns {Date} The parsed date. */ unformat(value: string): Date; /** * Converts a Date object to a formatted string. * * @param {Date} value - The Date to format. * @returns {string} Formatted date string. */ format(value: Date): string; }