# Date range picker

> Two dates in one field, with two months side by side and quick ranges. A Pyzed component for Python desktop GUIs, with examples and its API.

Source: https://albakore.github.io/pyzed/library/date-range-picker/

:::tip[In plain words]
A box for two dates, from and to. You click where the period starts and where it ends.
:::

`ui.DateRangePicker` holds a start and an end date. Its popup shows two months: the first click picks the start, the pointer previews the range and the second click picks the end (in any order). The column on the left has quick ranges (today, last 7 days, this week, this month, last month...). `on_change` receives `(start, end)` as two `datetime.date`.

`ui.DateRangePicker` · Category: [Dates and times](https://albakore.github.io/pyzed/library/#dates-and-times) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#daterangepicker)

**Code:**

```python
pz.container(
    ui.DateRangePicker(value=("2026-10-05", "2026-10-14"), format="DD/MM/YYYY", on_change=on_change),
    width=260, height=330, align_y="start")
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- For reports, filters, stays and anything that spans days.
- `months=1` shows a single month and `presets=False` hides the quick ranges.
- The field can be typed too: two dates in `format` with any separator.

## Usage

```python
import pyzed as pz
from pyzed import ui

class Report(pz.App):
    def __init__(self):
        self.period = (None, None)

    def view(self):
        start, end = self.period
        days = (end - start).days + 1 if start and end else 0
        return pz.column([
            ui.DateRangePicker(value=self.period, format="MMM D, YYYY",
                               on_change=lambda e: setattr(self, "period", e.value)),
            ui.label(f"{days} days selected", color="text_muted"),
        ], spacing=8, width=300)
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Popup

```python
ui.DateRangePicker(value=("2026-10-05", "2026-10-14"), on_change=on_change)
```

```python
ui.DateRangePicker(months=1, presets=False, on_change=on_change)
```

## Events

Date range picker has **3 events of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

It is a `pz.Component`, so it also raises `on_mount` and `on_unmount`, and its handlers get a `pz.Event` whose `value` holds the data.

| Event | Handler gets | When |
| --- | --- | --- |
| `on_change` | `(event)` | Receives `event.value`: `(start, end)`. |
| `on_open` | `(event)` | The calendar opened. |
| `on_close` | `(event)` | The calendar closed. |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Report(pz.App):
    def __init__(self):
        self.period = ("2026-10-05", "2026-10-14")

    def changed(self, event):            # on_change: event.value is (start, end)
        self.period = event.value

    def view(self):
        start, end = self.period
        return pz.column([
            ui.DateRangePicker(value=self.period, on_change=self.changed),
            ui.label(f"From {ui.format_date(start, 'D MMM')} to {ui.format_date(end, 'D MMM')}"),
        ], spacing=8, width=280)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Period:
    days: tuple

class Report(pz.App):
    period = (None, None)

    def update(self, message):
        match message:
            case Period(days):
                self.period = days

    def view(self):
        return ui.DateRangePicker(value=self.period, on_change=lambda e: Period(e.value))
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    period: tuple = (None, None)

def view(state, set_state):
    return ui.DateRangePicker(value=state.period, months=1, presets=False,
                              on_change=lambda e: set_state(period=e.value))
```

## Properties and API

### `ui.DateRangePicker`

```python
class DateRangePicker
```

A component: a field for a start and an end date, with two months and quick ranges (Today, Last 7 days, This month...) in its popup. Typed text is two dates separated by `separator`. See the [Date range picker](https://albakore.github.io/pyzed/library/date-range-picker/) page.

| Property | Default | Description |
| --- | --- | --- |
| `value` | `None` | `(start, end)`: dates, ISO text or `None`. |
| `format` | `"YYYY-MM-DD"` | How each date is written. |
| `separator` | `" – "` | Text between the two dates. |
| `placeholder` | `""` | Hint while empty. |
| `min` | `None` | The earliest date. |
| `max` | `None` | The latest date. |
| `disabled_dates` | `()` | Days that cannot be picked. |
| `first_weekday` | `"monday"` | `"monday"`, `"sunday"` or `"saturday"`. |
| `months` | `2` | Months shown side by side. |
| `presets` | `True` | Shows the quick ranges. |
| `clearable` | `True` | Shows an x that empties it. |
| `disabled` | `False` | Greys it out. |
| `invalid` | `False` | Paints the border in the error color. |

Events: `on_change`, `on_open`, `on_close` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#daterangepicker)

## Related

- [Date picker](https://albakore.github.io/pyzed/library/date-picker/): A date field with a calendar that drops down, in the format you choose.
- [Calendar](https://albakore.github.io/pyzed/library/calendar/): A month grid to pick a day, a range or several days, always on screen.
