feat: refs #7353 imporve toDateFormat
This commit is contained in:
parent
966667bab7
commit
ef1ba2ea4f
|
@ -20,21 +20,21 @@ export function isValidDate(date) {
|
||||||
* Converts a given date to a specific format.
|
* Converts a given date to a specific format.
|
||||||
*
|
*
|
||||||
* @param {number|string|Date} date - The date to be formatted.
|
* @param {number|string|Date} date - The date to be formatted.
|
||||||
|
* @param {Object} opts - Optional parameters to customize the output format.
|
||||||
* @returns {string} The formatted date as a string in 'dd/mm/yyyy' format. If the provided date is not valid, an empty string is returned.
|
* @returns {string} The formatted date as a string in 'dd/mm/yyyy' format. If the provided date is not valid, an empty string is returned.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* // returns "02/12/2022"
|
* // returns "02/12/2022"
|
||||||
* toDateFormat(new Date(2022, 11, 2));
|
* toDateFormat(new Date(2022, 11, 2));
|
||||||
*/
|
*/
|
||||||
export function toDateFormat(date, locale = 'es-ES') {
|
export function toDateFormat(date, locale = 'es-ES', opts = {}) {
|
||||||
if (!isValidDate(date)) {
|
if (!isValidDate(date)) return '';
|
||||||
return '';
|
|
||||||
}
|
const format = Object.assign(
|
||||||
return new Date(date).toLocaleDateString(locale, {
|
{ year: 'numeric', month: '2-digit', day: '2-digit' },
|
||||||
year: 'numeric',
|
opts
|
||||||
month: '2-digit',
|
);
|
||||||
day: '2-digit',
|
return new Date(date).toLocaleDateString(locale, format);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue