es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
21 lines (20 loc) • 822 B
JavaScript
/**
* Formats the date according to the specified format string.
* @param {Date} date - The date to format.
* @param {string} formatStr - The format string (e.g., 'YYYY-MM-DD hh:mm:ss').
* @returns {string} The formatted date string.
* @example
* const date = new Date('2023-05-01T12:30:45');
* const formatted = format(date, 'YYYY-MM-DD hh:mm:ss'); // '2023-05-01 12:30:45'
*/
export function format(date, formatStr) {
const map = {
YYYY: date.getFullYear().toString(),
MM: ('0' + (date.getMonth() + 1)).slice(-2),
DD: ('0' + date.getDate()).slice(-2),
hh: ('0' + date.getHours()).slice(-2),
mm: ('0' + date.getMinutes()).slice(-2),
ss: ('0' + date.getSeconds()).slice(-2)
};
return formatStr.replace(/YYYY|MM|DD|hh|mm|ss/g, matched => map[matched]);
}