mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-15 12:36:30 +01:00
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
import { i18n } from '@lingui/core';
|
|
|
|
import localeMatch from './locale-match';
|
|
import mem from './mem';
|
|
|
|
const defaultLocale = new Intl.DateTimeFormat().resolvedOptions().locale;
|
|
|
|
const _DateTimeFormat = (opts) => {
|
|
const { locale, dateYear, hideTime, formatOpts } = opts || {};
|
|
const loc =
|
|
locale && !/pseudo/i.test(locale)
|
|
? localeMatch([locale], [defaultLocale])
|
|
: defaultLocale;
|
|
const currentYear = new Date().getFullYear();
|
|
const options = {
|
|
// Show year if not current year
|
|
year: dateYear === currentYear ? undefined : 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
// Hide time if requested
|
|
hour: hideTime ? undefined : 'numeric',
|
|
minute: hideTime ? undefined : 'numeric',
|
|
...formatOpts,
|
|
};
|
|
try {
|
|
return Intl.DateTimeFormat(loc, options);
|
|
} catch (e) {
|
|
return Intl.DateTimeFormat(undefined, options);
|
|
}
|
|
};
|
|
const DateTimeFormat = mem(_DateTimeFormat);
|
|
|
|
function niceDateTime(date, dtfOpts) {
|
|
if (!(date instanceof Date)) {
|
|
date = new Date(date);
|
|
}
|
|
const DTF = DateTimeFormat({
|
|
dateYear: date.getFullYear(),
|
|
locale: i18n.locale,
|
|
...dtfOpts,
|
|
});
|
|
const dateText = DTF.format(date);
|
|
return dateText;
|
|
}
|
|
|
|
export default niceDateTime;
|