DateRangePicker
The DateRangePicker control combines an input text field with a RangeCalendar which allows displaying a date range selected by a user in the input. You can align the popup with calendars relative to the input, set the width of the popup, apply the desired date format, use a placeholder in the input, display a selected date range and hide calendar buttons.

Related resources
- Get the widget by installing the
@svar-ui/svelte-corepackage. - Check DateRangePicker API Reference to see the list of configuration properties and events.
- Explore the samples to visualize the available features.
Initialization
To create a DateRangePicker instance on a page, you need to complete two steps:
- import the source file of the control on a page:
<script>
import { DateRangePicker } from "@svar-ui/svelte-core";
</script>
- apply the
<DateRangePicker>tag to initialize a date range picker:
<DateRangePicker />
A default date range picker is initialized with no date range selected.
Setting the value
You can select a particular date range in calendars and show it in the input of a date range picker. Use the value property for this purpose. The value of this property is an object with the start and end dates of a range. Set each date as a Date object.
<script>
const date = {
start: new Date(2024, 1, 1),
end: new Date(2024, 3, 3)
};
</script>
<DateRangePicker value={date} />
The specified date range will appear in the date range picker input and the days of the range will be highlighted in the calendars, if visible.

Related sample: DateRangePicker
Getting the current value
You can get the current value of the DateRangePicker control. For this, you need to:
- declare a variable that will contain the control's value (it may contain the initial value):
<script>
import { DateRangePicker } from "@svar-ui/svelte-core";
let daterange= {
start: new Date(2020, 1, 1),
end: new Date(2021, 3, 3),
};
</script>
- bind the variable to the
valueproperty of the control:
<DateRangePicker bind:value={daterange} />
If the name of the variable matches its value, you can use the shorthand while binding the property:
<script>
import { DateRangePicker } from "@svar-ui/svelte-core";
let value;
</script>
<DateRangePicker bind:value />
After that the value assigned to the bound variable will be updated on each change of the date range.
Catching the change of the selected date
In case you need to catch the change of the selected date range, you can do it by handling the change event. Inside the event you can get the newly selected date range in the following way:
<script>
function handleSelect({value}) {
console.log(value);
// an object with the start and end dates of the selected range
}
</script>
<DateRangePicker onchange={handleSelect} />
The handler function of the change event takes an object with the new value of the date range picker (start and end dates of the selected range).
Related sample: DateRangePicker
Setting the number of calendars
A default DateRangePicker has 2 calendars for selection of a date range. You can also use one month to select a date range by specifying the months property with the 1 value.
<DateRangePicker months={1} />

Related sample: DateRangePicker
Setting the date format
The default format of displaying a date in DateRangePicker is "%m/%d/%Y". You can change it and set your own format with the help of the format property.
<DateRangePicker value={date} format="%d %F, %Y" />
SVAR DateRangePicker uses the date-fns library for handling dates. You can find format specifications in the corresponding article.

Related sample: DateRangePicker
Configuring the dropdown
You can configure the behavior and appearance of the date range picker dropdown via the dropdown property.
By default, the dropdown is attached to the document body and rendered above the other elements of the application. If you use a custom popupContainer, the dropdown will be attached to it instead. To render the dropdown inline — inside the same HTML element as the input — set the inline option to true:
<DateRangePicker dropdown={{ inline: true }} />
The popup with calendars is automatically aligned to the bottom left of the input by default. You can control its alignment with the align option:
<DateRangePicker dropdown={{ align: "center" }} />

The width of the popup adjusts to the calendars inside it by default. If needed, you can change the popup's width using the width option. It works in the same way as the width CSS property:
<DateRangePicker dropdown={{ width: "100%" }} />

When the dropdown is in the default (non-inline) mode and the date range picker is placed inside a scrollable container, you can use the trackScroll option to automatically close the dropdown when the container is scrolled. This prevents the dropdown from staying open after the input has scrolled out of view:
<Field>
<div style="height: 150px; overflow: auto;">
<div style="height: 700px;">
<DateRangePicker dropdown={{ trackScroll: true }} />
</div>
</div>
</Field>
Related article: dropdown
Related sample: DateRangePicker
Configuring buttons
Hiding the Today and Clear buttons
The calendars of DateRangePicker have the Today and Clear buttons in the lower part. If needed, you can switch those buttons off. Set the buttons property to false to achieve this purpose:
<DateRangePicker buttons={false} />

Enabling individual buttons
Instead of showing all default buttons, you can display only the specific buttons you need — "clear", "today", or "done". To do this, pass an array to the buttons property containing the names of the buttons you want to enable.
For example, to display only the Clear and Done buttons:
<DateRangePicker value={date} buttons={["clear", "done"]} />
Related sample: DateRangePicker
Setting the date format
The default format of displaying a date in the DateRangePicker input in the EN locale is "%m/%d/%Y". You can change it and set your own format with the help of the format property. Set the value of this property as a string with the format or as a function that returns a formatted string.
<DateRangePicker value={date} format="%d %F, %Y" />
Check the characters available for specifying the date format.

Related sample: DateRangePicker
You can also specify the date format from the locale.