aboutsummaryrefslogtreecommitdiff
path: root/src/components/common/DateDisplay.tsx
blob: 0bece8aed9ec9d60d75ae4ffc888014782ddaf01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Icon, Row, Text } from '@umami/react-zen';
import { differenceInDays, isSameDay } from 'date-fns';
import { useLocale } from '@/components/hooks';
import { Calendar } from '@/components/icons';
import { formatDate } from '@/lib/date';

export function DateDisplay({ startDate, endDate }) {
  const { locale } = useLocale();
  const isSingleDate = differenceInDays(endDate, startDate) === 0;

  return (
    <Row gap="3" alignItems="center" wrap="nowrap">
      <Icon>
        <Calendar />
      </Icon>
      <Text wrap="nowrap">
        {isSingleDate ? (
          formatDate(startDate, 'PP', locale)
        ) : (
          <>
            {formatDate(startDate, 'PP', locale)}
            {!isSameDay(startDate, endDate) && ` — ${formatDate(endDate, 'PP', locale)}`}
          </>
        )}
      </Text>
    </Row>
  );
}