sussy-util
Version:
Util package made by me
65 lines (64 loc) • 2.8 kB
TypeScript
export default class DateTimeFormatter {
readonly pattern: string;
private constructor();
/**
* Creates a new DateTimeFormatter instance with the provided pattern.
* @param {string} pattern - The pattern to use for formatting and parsing dates/times.
* @returns {DateTimeFormatter} A new instance of DateTimeFormatter.
* @throws {Error} Throws an error if the pattern is not a non-empty string.
*/
static ofPattern: (pattern: string) => DateTimeFormatter;
/**
* Gets the pattern used by this formatter.
* @returns {string} The pattern string.
*/
getPattern: () => string;
/**
* Formats the date portion (YYYY, MM, DD) of a given Date object.
* @param {Date} date - The date object to format.
* @returns {string} The formatted date string.
* @throws {Error} Throws an error if the provided date is invalid.
*/
formatDate: (date: Date) => string;
/**
* Formats both the date and time based on the pattern.
* @param {Date} date - The date object to format.
* @returns {string} The formatted date-time string.
* @throws {Error} Throws an error if the provided date is invalid.
*/
formatDateTime: (date: Date) => string;
/**
* Parses a date string based on the formatter's pattern.
* @param {string} dateString - The date string to parse.
* @returns {Date} The parsed Date object.
* @throws {Error} Throws an error if the string is invalid or the pattern is incorrect.
*/
parseDate: (dateString: string) => Date;
/**
* Parses a time string based on the formatter's pattern.
* @param {string} timeString - The time string to parse.
* @returns {Object} An object containing hours, minutes, and seconds.
* @throws {Error} Throws an error if the string is invalid or the pattern is incorrect.
*/
parseTime: (timeString: string) => {
hours: number;
minutes: number;
seconds: number;
};
/**
* Parses a combined date-time string based on the formatter's pattern.
* @param {string} dateTimeString - The date-time string to parse.
* @returns {Date} The parsed Date object.
* @throws {Error} Throws an error if the string is invalid or the pattern is incorrect.
*/
parseDateTime: (dateTimeString: string) => Date;
/**
* Extracts a substring from the given dateTime string based on tokens.
* @private
* @param {string} dateTimeString - The full dateTime string.
* @param {'YYYY' | 'HH' | 'hh'} startToken - The starting token for extraction.
* @param {'DD' | 'ss'} endToken - The ending token for extraction.
* @returns {string|null} The extracted substring, or null if tokens are not found.
*/
private extractPart;
}