Documentation

Install

npm install -D date-picker-svelte

Usage

<script>
	import { DateInput } from 'date-picker-svelte'
	let date = new Date()
</script>

<DateInput bind:value={date} />

DateInput

Component with an input field that shows the DatePicker component on focus. The component will not assign a date value until a specific date is selected in the picker or entered into the field.

Props

PropTypeDescription
valueDate | nullDate value
initialBrowseDateDateInitial date to show in the calendar when no value is selected
minDateThe earliest value the user can select
maxDateThe latest value the user can select
placeholderstringPlaceholder used when date value is null
timePrecision"minute" | "second" | "millisecond" | nullShow a time picker with the specified precision
idstring | nullSet the input element's ID attribute
validboolWhether the text is valid
formatstringFormat string
visibleboolWhether the date popup is visible
disabledboolDisable the input
requiredboolRequire a value to submit form
closeOnSelectionboolClose the date popup when a date is selected
browseWithoutSelectingboolWait with updating the date until a value is selected
dynamicPositioningboolDynamically postions the date popup to best fit on the screen
localeLocaleLocale object for internationalization
isDisabledDate((dateToCheck: Date) => boolean) | nullDisallow specific dates

Format string

Example format string: yyyy-MM-dd HH:mm:ss

PatternResult example
yyyy2021
yy21
MM12
dd31
HH23
mm59
ss59

DatePicker

Component with a calendar for choosing a date. The component will not assign a date value until a specific date is selected in the picker.

Props

PropTypeDescription
valueDate | nullDate value
initialBrowseDateDateInitial date to show in the calendar when no value is selected
minDateThe earliest year the user can select
maxDateThe latest year the user can select
timePrecision"minute" | "second" | "millisecond" | nullShow a time picker with the specified precision
localeLocaleLocale object for internationalization
browseWithoutSelectingboolWait with updating the date until a date is selected
isDisabledDate((dateToCheck: Date) => boolean) | nullDisallow specific dates

Date disabling example

Example usage of the isDisabledDate prop:

<script>
	const disabledDate = new Date()
</script>

<DatePicker
	isDisabledDate={(dateToCheck) => {
		return isSameDate(dateToCheck, disabledDate)
	}}
/>

Internationalization

`Locale`

Object to support internationalization. Properties (all are optional):

  • weekdays: Array of weekdays in short form, Sunday to Monday. Default: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
  • months: Array of month names, January to December. Default: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  • weekStartsOn: The day the week starts on, 0 = Sunday. Default: 1

`localeFromDateFnsLocale`

If you use date-fns, you can create a Locale object by passing a date-fns locale to this function:

<script>
	import { DatePicker, localeFromDateFnsLocale } from 'date-picker-svelte'
	import { hy } from 'date-fns/locale'
	let date = new Date()
	let locale = localeFromDateFnsLocale(hy)
</script>

<DatePicker bind:value={date} {locale} />

CSS variables

Colors:

  • --date-picker-foreground
  • --date-picker-background
  • --date-picker-highlight-border
  • --date-picker-highlight-shadow
  • --date-picker-selected-color
  • --date-picker-selected-background

Lengths:

  • --date-input-width

Dark theme example:

:root {
	--date-picker-background: #1b1e27;
	--date-picker-foreground: #f7f7f7;
}

Custom CSS

You can always use :global() to add your own styles. Example:

:global(.date-time-picker) {
	width: 16rem;
}