Message: Proper date formatting based on locale (#1751)
This commit is contained in:
parent
5921e9770a
commit
b52ee4afe5
@ -65,26 +65,17 @@ export function formatPastTimeShort(lang: LangFn, datetime: number | Date) {
|
|||||||
return lang(`Weekday.Short${WEEKDAYS_FULL[date.getDay()]}`);
|
return lang(`Weekday.Short${WEEKDAYS_FULL[date.getDay()]}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const withYear = date.getFullYear() !== today.getFullYear();
|
const noYear = date.getFullYear() === today.getFullYear();
|
||||||
const format = (
|
|
||||||
lang(withYear ? 'formatDateScheduleYear' : 'formatDateSchedule')
|
|
||||||
|| (withYear ? 'd MMM yyyy' : 'd MMM')
|
|
||||||
);
|
|
||||||
|
|
||||||
return formatDate(lang, date, format);
|
return formatDateToString(date, lang.code, noYear);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatFullDate(lang: LangFn, datetime: number | Date) {
|
export function formatFullDate(lang: LangFn, datetime: number | Date) {
|
||||||
const date = typeof datetime === 'number' ? new Date(datetime) : datetime;
|
return formatDateToString(datetime, lang.code, false, 'numeric');
|
||||||
const format = lang('formatterYearMax') || 'dd.MM.yyyy';
|
|
||||||
|
|
||||||
return formatDate(lang, date, format);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatMonthAndYear(lang: LangFn, date: Date, isShort = false) {
|
export function formatMonthAndYear(lang: LangFn, date: Date, isShort = false) {
|
||||||
const format = lang(isShort ? 'formatterMonthYear2' : 'formatterMonthYear') || 'MMM yyyy';
|
return formatDateToString(date, lang.code, false, isShort ? 'short' : 'long', true);
|
||||||
|
|
||||||
return formatDate(lang, date, format);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatCountdown(
|
export function formatCountdown(
|
||||||
@ -164,29 +155,10 @@ export function formatHumanDate(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const withYear = date.getFullYear() !== today.getFullYear();
|
const noYear = date.getFullYear() === today.getFullYear();
|
||||||
const formatKey = isShort
|
const formattedDate = formatDateToString(date, lang.code, noYear, isShort ? 'short' : 'long');
|
||||||
? (withYear ? 'formatDateScheduleYear' : 'formatDateSchedule')
|
|
||||||
: (withYear ? 'chatFullDate' : 'chatDate');
|
|
||||||
const format = lang(formatKey) || 'd MMMM yyyy';
|
|
||||||
|
|
||||||
return (isUpperFirst || !isShort ? upperFirst : lowerFirst)(formatDate(lang, date, format));
|
return (isUpperFirst || !isShort ? upperFirst : lowerFirst)(formattedDate);
|
||||||
}
|
|
||||||
|
|
||||||
function formatDate(lang: LangFn, date: Date, format: string) {
|
|
||||||
const day = date.getDate();
|
|
||||||
const monthIndex = date.getMonth();
|
|
||||||
|
|
||||||
return format
|
|
||||||
.replace('LLLL', lang(MONTHS_FULL[monthIndex]))
|
|
||||||
.replace('MMMM', lang(`Month.Gen${MONTHS_FULL[monthIndex]}`))
|
|
||||||
.replace('MMM', lang(`Month.Short${MONTHS_FULL[monthIndex]}`))
|
|
||||||
.replace('MM', String(monthIndex + 1).padStart(2, '0'))
|
|
||||||
.replace('dd', String(day).padStart(2, '0'))
|
|
||||||
.replace('d', String(day))
|
|
||||||
.replace('yyyy', String(date.getFullYear()))
|
|
||||||
// Workaround for https://bugs.telegram.org/c/5777
|
|
||||||
.replace(/'de'/g, 'de');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatMediaDateTime(
|
export function formatMediaDateTime(
|
||||||
@ -247,22 +219,34 @@ export function formatVoiceRecordDuration(durationInMs: number) {
|
|||||||
return `${parts.join(':')},${String(milliseconds).padStart(2, '0')}`;
|
return `${parts.join(':')},${String(milliseconds).padStart(2, '0')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatDayToStringWithCache = withCache((dayStartAt: number, locale: string) => {
|
const formatDayToStringWithCache = withCache((
|
||||||
|
dayStartAt: number,
|
||||||
|
locale: string,
|
||||||
|
noYear?: boolean,
|
||||||
|
monthFormat: 'short' | 'long' | 'numeric' = 'short',
|
||||||
|
noDay?: boolean,
|
||||||
|
) => {
|
||||||
return new Date(dayStartAt).toLocaleString(
|
return new Date(dayStartAt).toLocaleString(
|
||||||
locale,
|
locale,
|
||||||
{
|
{
|
||||||
year: 'numeric',
|
year: noYear ? undefined : 'numeric',
|
||||||
month: 'short',
|
month: monthFormat,
|
||||||
day: 'numeric',
|
day: noDay ? undefined : 'numeric',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
export function formatDateToString(datetime: Date | number, locale = 'en-US') {
|
export function formatDateToString(
|
||||||
|
datetime: Date | number,
|
||||||
|
locale = 'en-US',
|
||||||
|
noYear = false,
|
||||||
|
monthFormat: 'short' | 'long' | 'numeric' = 'short',
|
||||||
|
noDay = false,
|
||||||
|
) {
|
||||||
const date = typeof datetime === 'number' ? new Date(datetime) : datetime;
|
const date = typeof datetime === 'number' ? new Date(datetime) : datetime;
|
||||||
const dayStartAt = getDayStartAt(date);
|
const dayStartAt = getDayStartAt(date);
|
||||||
|
|
||||||
return formatDayToStringWithCache(dayStartAt, locale);
|
return formatDayToStringWithCache(dayStartAt, locale, noYear, monthFormat, noDay);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatDateTimeToString(datetime: Date | number, locale = 'en-US') {
|
export function formatDateTimeToString(datetime: Date | number, locale = 'en-US') {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user