2023-07-08 08:43:25 +03:00
|
|
|
const { locale } = new Intl.DateTimeFormat().resolvedOptions();
|
|
|
|
|
2023-03-03 13:11:37 +03:00
|
|
|
function niceDateTime(date, { hideTime, formatOpts } = {}) {
|
2023-03-01 15:07:22 +03:00
|
|
|
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',
|
2023-03-03 13:11:37 +03:00
|
|
|
...formatOpts,
|
2023-03-01 15:07:22 +03:00
|
|
|
}).format(date);
|
|
|
|
return dateText;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default niceDateTime;
|