DatePicker
The DatePicker control is a combination of an input text field and a drop-down calendar. The calendar is shown on click in the input. A date selected in the calendar appears in the input field. You can align the popup calendar relative to the input, set the popup's width, specify the date format, set the selected date and manipulate visibility of calendar buttons.

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

Related sample: DatePicker
Getting the current value
You can get the current value of the DatePicker control. For this, you need to:
- declare a variable that will contain the control's value (it may contain the initial value):
<script>
import { DatePicker } from "@svar-ui/svelte-core";
let date;
</script>
- bind the variable to the
valueproperty of the control:
<DatePicker bind:value={date} />
If the name of the variable matches its value, you can use the shorthand while binding the property:
<script>
import { DatePicker } from "@svar-ui/svelte-core";
let value;
</script>
<DatePicker bind:value />
After that the value assigned to the bound variable will be updated on each change of the date.
Catching the change of the selected date
In case you need to catch the change of the selected date, you can do it by handling the
change event. Inside the event you can get the newly selected date in the following way:
<script>
function handleSelect({value}){
console.log(value);
// date object
}
</script>
<DatePicker onchange={handleChange} />
The handler function of the change event receives a date object with a new date as a new value of the date picker.
Related sample: DatePicker
Setting the date format
The default format of displaying a date in DatePicker is "%m/%d/%Y". You can change it and set your own format with the help of the format property.
<DatePicker value={new Date()} format="%d %F, %Y" />

SVAR DatePicker uses the date-fns library for handling dates. You can find format specifications in the corresponding article.
Related sample: DatePicker
Aligning the calendar
The popup with a calendar which appears on click in the input of DatePicker is automatically aligned to the bottom left of the input. You can control its alignment with the align property.
<DatePicker align="center"/>
In the above example the popup is aligned to the center of the input field.

Related sample: DatePicker
Adjusting the calendar width
The popup of DatePicker adjusts its width to the calendar that is placed inside it. If needed, you can change the width of the popup using the width property. It works in the same way as the width CSS property. The value should be set as a string, e.g.:
<DatePicker width="100%" />

Related sample: DatePicker