@cc-heart/utils
Version:
🔧 javascript common tools collection
59 lines (58 loc) • 2.88 kB
TypeScript
/**
* Returns the current time in ISO string format.
* @returns The current time in ISO string format.
*/
export declare function getCurrentTimeISOString(): string;
/**
* Formats a date based on a given timestamp.
*
* @param timeStamp - The timestamp to be formatted, in milliseconds.
* @param formatter - Optional. A specific format string to format the date. Defaults to 'YYYY-MM-DD HH:mm:ss'.
*
* @returns The formatted date string.
*
* @throws {Error} Throws an error if the timestamp is invalid or out of range.
*
* @example
* const timestamp = new Date().getTime();
* const formattedDate = formatDateByTimeStamp(timestamp);
* console.log(formattedDate);
*/
export declare function formatDateByTimeStamp(timeStamp: number, formatter?: string): string;
/**
* Formats a date string into a specified date time format
*
* @param dateString - A string representing the date.
* @param formatter - Optional. A specific format string to format the date. Defaults to 'YYYY-MM-DD HH:mm:ss'.
*
* @returns The formatted date string.
*
* @throws {Error} Throws an error if the date string is invalid or cannot be parsed into a Date object.
*
* @example
* const dateString = '2024-02-19T10:30:00Z';
* const formattedDateTime = formatDateTimeByString(dateString, 'MMMM D, YYYY, h:mm A');
* console.log(formattedDateTime);
*/
export declare function formatDateTimeByString(dateString: string, formatter?: string): string;
/**
* Formats a date based on an array of numbers, with optional formatting string
*
* This function expects an array containing year, month, day, hour, minute, and second values. If the array is invalid or does not contain the necessary values, it logs a warning and returns 'Invalid Date'.
* The function uses the slice method to create a new array containing only the first six elements, and decrements the month value by 1 to convert it from a 1-based index to a 0-based index used by the Date object.
* It then creates a new Date object using the elements of the new array. If the date is invalid, it logs a warning and returns the date as a string.
* Finally, the function formats the date according to an optional formatting string and returns the formatted date string.
*
* @param array - The array representing the date. This array should contain year, month, day, hour, minute, and second values.
* @param formatter - Optional. A specific format string to format the date. Defaults to 'YYYY-MM-DD HH:mm:ss'.
*
* @returns The formatted date string.
*
* @throws {Error} Throws an error if the array is invalid or does not contain the necessary values.
*
* @example
* const dateArray = [2024, 2, 19, 10, 30, 0];
* const formattedDate = formatDateByArray(dateArray, 'MMMM D, YYYY, h:mm A');
* console.log(formattedDate);
*/
export declare function formatDateByArray(array: number[], formatter?: string): string;