@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
59 lines (57 loc) • 1.46 kB
JavaScript
import { getCachedIntl } from "../utils/intl.mjs";
import { internationalization } from "@intlayer/config/built";
//#region src/formatters/date.ts
const presets = {
short: {
year: "2-digit",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit"
},
long: {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric"
},
full: {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: false
},
dateOnly: {
year: "numeric",
month: "short",
day: "numeric"
},
timeOnly: {
hour: "numeric",
minute: "numeric",
second: "numeric"
}
};
/**
* Formats a date/time value into a localized string using Intl.DateTimeFormat.
*
* @example
* date(new Date('2025-08-02T14:30:00Z'), { year: '2-digit', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' });
* // "08/02/25, 14:30"
*
* @example
* date("2025-08-02T14:30:00Z", { locale: Locales.FRENCH, month: "long", day: "numeric" });
* // "2 août"
*/
const date = (date, options) => {
const dateTime = new Date(date);
const resolvedOptions = typeof options === "string" ? presets[options] ?? {} : options;
const locale = (typeof options === "object" ? options?.locale : void 0) ?? internationalization?.defaultLocale;
return getCachedIntl(Intl.DateTimeFormat, locale, resolvedOptions).format(dateTime);
};
//#endregion
export { date, presets };
//# sourceMappingURL=date.mjs.map