PPolaris ComponentsConnect an agent

Date picker

A month-by-month calendar for choosing a single date or a date range, used for scheduling discounts, filtering orders and picking report periods.

Preview

Code

Example.jsx
import { useState, useCallback } from 'react';
import { Card, BlockStack, InlineStack, Text, DatePicker, Checkbox, Badge } from '@shopify/polaris';

const INITIAL_MONTH = { month: 5, year: 2026 };

const INITIAL_RANGE = {
  start: new Date(2026, 5, 8),
  end: new Date(2026, 5, 14),
};

const DATE_FORMAT = { month: 'short', day: 'numeric', year: 'numeric' };

const DISABLE_BEFORE = new Date(2026, 5, 1);

const formatDate = (date) => date.toLocaleDateString('en-US', DATE_FORMAT);

export const Example = () => {
  const [{ month, year }, setVisibleMonth] = useState(INITIAL_MONTH);
  const [selectedDates, setSelectedDates] = useState(INITIAL_RANGE);
  const [allowRange, setAllowRange] = useState(true);

  const handleMonthChange = useCallback((nextMonth, nextYear) => {
    setVisibleMonth({ month: nextMonth, year: nextYear });
  }, []);

  const handleDateChange = useCallback((range) => {
    setSelectedDates(range);
  }, []);

  const handleAllowRangeChange = useCallback((checked) => {
    setAllowRange(checked);
    if (!checked) {
      setSelectedDates((range) => ({ start: range.start, end: range.start }));
    }
  }, []);

  const isRange = selectedDates.start.getTime() !== selectedDates.end.getTime();

  return (
    <Card>
      <BlockStack gap="400">
        <BlockStack gap="100">
          <Text as="h2" variant="headingMd">
            Discount active dates
          </Text>
          <Text as="p" tone="subdued">
            Pick when the Summer sale discount is available to buyers.
          </Text>
        </BlockStack>

        <DatePicker
          month={month}
          year={year}
          onChange={handleDateChange}
          onMonthChange={handleMonthChange}
          selected={selectedDates}
          disableDatesBefore={DISABLE_BEFORE}
          allowRange={allowRange}
          multiMonth={false}
        />

        <Checkbox
          label="Set an end date"
          checked={allowRange}
          onChange={handleAllowRangeChange}
          helpText="Turn this off to run the discount from a start date with no end."
        />

        <InlineStack gap="200" blockAlign="center">
          <Badge tone="success">Selected</Badge>
          <Text as="span" fontWeight="medium">
            {isRange
              ? `${formatDate(selectedDates.start)} – ${formatDate(selectedDates.end)}`
              : formatDate(selectedDates.start)}
          </Text>
        </InlineStack>
      </BlockStack>
    </Card>
  );
};