Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,12 +1185,32 @@ function getFormattedSplitDateRange(
}

/**
* Converts a date to a string in the format MMMM d, yyyy
* Building an `Intl.DateTimeFormat` costs far more than formatting with one, so instances are cached per language.
* They format in UTC, so pass dates through `toUTCWallClockDate` to show them in the device's current timezone.
*/
const formatToReadableString = (date: string, dateFnsLocale: DateFnsLocale | undefined): string => {
const parsedDate = parse(date, 'yyyy-MM-dd', new Date());
return format(parsedDate, 'MMMM d, yyyy', {locale: dateFnsLocale});
};
const memoizedCreateLongDateFormatter = memoize((localeCode: string) => new Intl.DateTimeFormat(localeCode, {year: 'numeric', month: 'long', day: 'numeric', timeZone: 'UTC'}));

/**
* Formats a date's local calendar fields as a long date.
*
* A hand-written `MMMM d, yyyy` fixes the US component order, so German showed its month name before the day and
* Spanish lost the `de` it writes between the parts of a date. `Intl` orders the date the way each language does, and
* its English output matches the old pattern.
*
* @returns June 20, 2026
*/
function formatToLongDate(date: Date, dateFnsLocale: DateFnsLocale | undefined): string {
if (!isValid(date)) {
return '';
}
// date-fns falls back to en-US when given no locale, so this matches the date-fns formatters elsewhere in this file.
return memoizedCreateLongDateFormatter(dateFnsLocale?.code ?? 'en-US').format(toUTCWallClockDate(date));
}

/**
* Converts a yyyy-MM-dd date to a long date, e.g. June 20, 2026
*/
const formatToReadableString = (date: string, dateFnsLocale: DateFnsLocale | undefined): string => formatToLongDate(parse(date, CONST.DATE.FNS_FORMAT_STRING, new Date()), dateFnsLocale);

const formatInTimeZoneWithFallback: typeof formatInTimeZone = (date, timeZone, formatStr, options?) => {
try {
Expand Down Expand Up @@ -1473,6 +1493,7 @@ const DateUtils = {
getTimeOfDayGreetingKey,
formatToLocalTime,
formatToReadableString,
formatToLongDate,
getZoneAbbreviation,
datetimeToRelative,
datetimeToCalendarTime,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/Subscription/CardSection/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type IconAsset from '@src/types/utils/IconAsset';
import type {Locale as DateFnsLocale} from 'date-fns';
import type {OnyxEntry} from 'react-native-onyx';

import {addMonths, format, fromUnixTime, startOfMonth} from 'date-fns';
import {addMonths, fromUnixTime, startOfMonth} from 'date-fns';

type BillingStatusResult = {
title: string;
Expand Down Expand Up @@ -215,7 +215,7 @@ function getNextBillingDate(dateFnsLocale: DateFnsLocale | undefined): string {

const nextBillingDate = startOfMonth(addMonths(today, 1));

return format(nextBillingDate, CONST.DATE.MONTH_DAY_YEAR_FORMAT, {locale: dateFnsLocale});
return DateUtils.formatToLongDate(nextBillingDate, dateFnsLocale);
}

export default {getBillingStatus, getNextBillingDate};
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/DateUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,45 @@ describe('DateUtils', () => {
});
});

describe('local long date', () => {
// cspell:ignore Juni junio
describe('formatToLongDate', () => {
it('keeps the English long date unchanged', () => {
// Given a local date and no locale, which falls back to en-US
// When it is formatted as a long date
// Then English keeps the `MMMM d, yyyy` shape it had before
expect(DateUtils.formatToLongDate(new Date(2026, 5, 20), undefined)).toBe('June 20, 2026');
});

it('orders the date the way each language does', () => {
// Given languages that do not write a long date in the US order
// When the same date is formatted in each
// Then German puts the day first, Spanish adds its `de` between the parts and Japanese leads with the year
const date = new Date(2026, 5, 20);
expect(DateUtils.formatToLongDate(date, de)).toBe('20. Juni 2026');
expect(DateUtils.formatToLongDate(date, es)).toBe('20 de junio de 2026');
expect(DateUtils.formatToLongDate(date, ja)).toBe('2026年6月20日');
});

it('returns an empty string for an invalid date', () => {
// Given a date that cannot be read
// When it is formatted
// Then nothing is shown instead of `Intl` throwing a RangeError into the render
expect(DateUtils.formatToLongDate(new Date('not a date'), undefined)).toBe('');
});
});

describe('formatToReadableString', () => {
it('shows a yyyy-MM-dd date on its own day in the language order', () => {
// Given a calendar date such as a search filter boundary or a distance rate start date
// When it is formatted
// Then the stored day is shown, ordered the way each language writes it
expect(DateUtils.formatToReadableString('2026-06-20', undefined)).toBe('June 20, 2026');
expect(DateUtils.formatToReadableString('2026-06-20', de)).toBe('20. Juni 2026');
});
});
});

describe('getFormattedReservationRangeDate', () => {
// cspell:ignore Dienstag Freitag März
beforeEach(() => {
Expand Down
Loading