mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 17:54:02 +00:00
fix(miniapp): keep date-only values stable across timezones
This commit is contained in:
@@ -4,13 +4,54 @@ function localeTag(locale: Locale): string {
|
|||||||
return locale === 'ru' ? 'ru-RU' : 'en-US'
|
return locale === 'ru' ? 'ru-RU' : 'en-US'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatCalendarDate(
|
||||||
|
year: number,
|
||||||
|
month: number,
|
||||||
|
day: number,
|
||||||
|
locale: Locale
|
||||||
|
): string | null {
|
||||||
|
if (
|
||||||
|
!Number.isInteger(year) ||
|
||||||
|
!Number.isInteger(month) ||
|
||||||
|
!Number.isInteger(day) ||
|
||||||
|
month < 1 ||
|
||||||
|
month > 12 ||
|
||||||
|
day < 1 ||
|
||||||
|
day > 31
|
||||||
|
) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const includeYear = year !== new Date().getUTCFullYear()
|
||||||
|
|
||||||
|
return new Intl.DateTimeFormat(localeTag(locale), {
|
||||||
|
month: 'long',
|
||||||
|
day: 'numeric',
|
||||||
|
...(includeYear ? { year: 'numeric' } : {}),
|
||||||
|
timeZone: 'UTC'
|
||||||
|
}).format(new Date(Date.UTC(year, month - 1, day)))
|
||||||
|
}
|
||||||
|
|
||||||
export function formatFriendlyDate(value: string, locale: Locale): string {
|
export function formatFriendlyDate(value: string, locale: Locale): string {
|
||||||
|
const calendarDateMatch = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
|
||||||
|
if (calendarDateMatch) {
|
||||||
|
const formatted = formatCalendarDate(
|
||||||
|
Number.parseInt(calendarDateMatch[1] ?? '', 10),
|
||||||
|
Number.parseInt(calendarDateMatch[2] ?? '', 10),
|
||||||
|
Number.parseInt(calendarDateMatch[3] ?? '', 10),
|
||||||
|
locale
|
||||||
|
)
|
||||||
|
if (formatted) {
|
||||||
|
return formatted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const date = new Date(value)
|
const date = new Date(value)
|
||||||
if (Number.isNaN(date.getTime())) {
|
if (Number.isNaN(date.getTime())) {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
const includeYear = date.getUTCFullYear() !== new Date().getUTCFullYear()
|
const includeYear = date.getFullYear() !== new Date().getFullYear()
|
||||||
|
|
||||||
return new Intl.DateTimeFormat(localeTag(locale), {
|
return new Intl.DateTimeFormat(localeTag(locale), {
|
||||||
month: 'long',
|
month: 'long',
|
||||||
|
|||||||
Reference in New Issue
Block a user