fix: add years to getDaysUntilText + confirmation number in upcoming stay card * fix: add years to getDaysUntilText + confirmation number in upcoming stay card Approved-by: Erik Tiekstra Approved-by: Matilda Landström
73 lines
1.6 KiB
TypeScript
73 lines
1.6 KiB
TypeScript
import { dt } from "@scandic-hotels/common/dt"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
import type { IntlShape } from "react-intl"
|
|
|
|
export function getDaysUntilText(
|
|
checkinDate: string,
|
|
lang: Lang,
|
|
intl: IntlShape
|
|
): string {
|
|
const checkInDateTime = dt(checkinDate).locale(lang).startOf("day")
|
|
const now = dt().locale(lang).startOf("day")
|
|
const daysUntil = checkInDateTime.diff(now, "days")
|
|
|
|
// Handle past dates edge case.
|
|
if (daysUntil < 0) {
|
|
return dt(checkinDate).locale(lang).format("D MMM YYYY")
|
|
}
|
|
|
|
if (daysUntil === 0) {
|
|
return intl.formatMessage({
|
|
id: "nextStay.today",
|
|
defaultMessage: "Today",
|
|
})
|
|
}
|
|
|
|
if (daysUntil === 1) {
|
|
return intl.formatMessage({
|
|
id: "nextStay.tomorrow",
|
|
defaultMessage: "Tomorrow",
|
|
})
|
|
}
|
|
|
|
if (daysUntil > 1 && daysUntil <= 30) {
|
|
return intl.formatMessage(
|
|
{
|
|
id: "nextStay.inXDays",
|
|
defaultMessage: "In {days} days",
|
|
},
|
|
{
|
|
days: daysUntil,
|
|
}
|
|
)
|
|
}
|
|
|
|
// Use proper month calculation for dates beyond 30 days
|
|
const monthsUntil = checkInDateTime.diff(now, "months")
|
|
|
|
if (monthsUntil <= 11) {
|
|
return intl.formatMessage(
|
|
{
|
|
id: "nextStay.inXMonths",
|
|
defaultMessage: "In {months, plural, one {# month} other { # months}}",
|
|
},
|
|
{
|
|
months: monthsUntil,
|
|
}
|
|
)
|
|
}
|
|
|
|
// 12+ months (show years)
|
|
const yearsUntil = checkInDateTime.diff(now, "years")
|
|
return intl.formatMessage(
|
|
{
|
|
id: "nextStay.inXYears",
|
|
defaultMessage: "In {years, plural, one {# year} other {# years}}",
|
|
},
|
|
{
|
|
years: yearsUntil,
|
|
}
|
|
)
|
|
}
|