phanpy/src/utils/nice-date-time.js

21 lines
647 B
JavaScript
Raw Normal View History

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',
...formatOpts,
2023-03-01 15:07:22 +03:00
}).format(date);
return dateText;
}
export default niceDateTime;