mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-24 11:18:14 +03:00
b02cae4967
Hopefully locale doesn't change half way
21 lines
643 B
JavaScript
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;
|