Skip to content

Commit

Permalink
- Add year to date description when not current year.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattias800 committed Sep 3, 2024
1 parent 9f00e0a commit 6b892dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export const useTravelDateInput = (
const valueWhenBlurred = useMemo(
() =>
selectedDate != null
? formatDateDescription(selectedDate, locale)
? formatDateDescription(selectedDate, today, locale)
: undefined,
[locale, selectedDate]
[locale, selectedDate, today]
);

const [visibleMonth, setVisibleMonth] = useState<Date>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export const useTravelDateRangeInput = (
const valueWhenBlurredStartDate = useMemo(
() =>
selectedStartDate != null
? formatDateDescription(selectedStartDate, locale)
? formatDateDescription(selectedStartDate, today, locale)
: undefined,
[locale, selectedStartDate]

Check warning on line 56 in packages/calendar/src/features/travel-calendar/hooks/UseTravelDateRangeInput.ts

View workflow job for this annotation

GitHub Actions / build

React Hook useMemo has a missing dependency: 'today'. Either include it or remove the dependency array

Check warning on line 56 in packages/calendar/src/features/travel-calendar/hooks/UseTravelDateRangeInput.ts

View workflow job for this annotation

GitHub Actions / build

React Hook useMemo has a missing dependency: 'today'. Either include it or remove the dependency array
);

const valueWhenBlurredEndDate = useMemo(
() =>
selectedEndDate != null
? formatDateDescription(selectedEndDate, locale)
? formatDateDescription(selectedEndDate, today, locale)
: undefined,
[locale, selectedEndDate]

Check warning on line 64 in packages/calendar/src/features/travel-calendar/hooks/UseTravelDateRangeInput.ts

View workflow job for this annotation

GitHub Actions / build

React Hook useMemo has a missing dependency: 'today'. Either include it or remove the dependency array

Check warning on line 64 in packages/calendar/src/features/travel-calendar/hooks/UseTravelDateRangeInput.ts

View workflow job for this annotation

GitHub Actions / build

React Hook useMemo has a missing dependency: 'today'. Either include it or remove the dependency array
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { format, Locale } from "date-fns";
import { format, isSameYear, Locale } from "date-fns";

export const formatDateDescription = (date: Date, locale: Locale) => {
export const formatDateDescription = (
date: Date,
today: Date,
locale: Locale
) => {
const year = isSameYear(date, today) ? "" : " y";
if (locale.code.startsWith("en")) {
return format(date, "eee MMM d", { locale });
return format(date, "eee MMM d" + year, { locale }).replace(".", "");
} else {
return format(date, "eee d MMM", { locale });
return format(date, "eee d MMM" + year, { locale }).replace(".", "");
}
};

export const formatDateDescriptionLong = (date: Date, locale: Locale) => {
export const formatDateDescriptionLong = (
date: Date,
today: Date,
locale: Locale
) => {
const year = isSameYear(date, today) ? "" : " y";
if (locale.code.startsWith("en")) {
return format(date, "eeee MMMM d", { locale });
return format(date, "eeee MMMM d" + year, { locale });
} else {
return format(date, "eeee d MMMM", { locale });
return format(date, "eeee d MMMM" + year, { locale });
}
};

0 comments on commit 6b892dc

Please sign in to comment.