phanpy/src/utils/nice-date-time.js
Lim Chee Aun b02cae4967 Try use more system locale
Hopefully locale doesn't change half way
2023-07-08 13:43:25 +08:00

21 lines
643 B
JavaScript

const { locale } = new Intl.DateTimeFormat().resolvedOptions();
function niceDateTime(date, { hideTime, formatOpts } = {}) {
if (!(date instanceof Date)) {
date = new Date(date);
}
const currentYear = new Date().getFullYear();
const dateText = Intl.DateTimeFormat(locale, {
// Show year if not current year
year: date.getFullYear() === currentYear ? undefined : 'numeric',
month: 'short',
day: 'numeric',
// Hide time if requested
hour: hideTime ? undefined : 'numeric',
minute: hideTime ? undefined : 'numeric',
...formatOpts,
}).format(date);
return dateText;
}
export default niceDateTime;