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 locale = new Intl.DateTimeFormat().resolvedOptions().locale;
|
|
|
|
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;
|