DatePicker日期选择框
DatePicker 用于选择日期、周、月、季度或年份,可带时间或多选。DateRangePicker 选择开始与结束日期。值均为原生 Date 对象。
何时使用
- 当日历有助于选择时使用:送货日、入住日期、报表周期等。
- 对于用户熟记的日期(如生日),也可直接输入:字段按
format解析文本。 - 相互关联的两个日期请使用
DateRangePicker,而不是两个独立控件。
代码演示
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
let date = $state<Date | null>(null);
</script>
<div class="stack">
<div class="field">
<DatePicker aria-label="Date" bind:value={date} />
<span role="status">Chosen: {date ? date.toDateString() : 'nothing'}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(260px, 100%);
}
</style>
picker="week"
picker="month"
picker="quarter"
picker="year"
<script lang="ts">
import { DatePicker, type PickerType } from 'svelte-ant-design';
const pickers: PickerType[] = ['week', 'month', 'quarter', 'year'];
</script>
<div class="stack">
{#each pickers as picker (picker)}
<div class="field">
<span class="caption">picker="{picker}"</span>
<DatePicker aria-label="Choose a {picker}" {picker} />
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(260px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
format="DD.MM.YYYY"
02.12.2026
format as a list: the first writes, each one reads
2 Dec 2026
format as a function
Wednesday, 2 December 2026
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
const today = new Date();
const long = (date: Date) => date.toLocaleDateString('en-GB', { dateStyle: 'full' });
</script>
<div class="stack">
<div class="field">
<span class="caption">format="DD.MM.YYYY"</span>
<DatePicker aria-label="Day, month, year" format="DD.MM.YYYY" value={today} />
</div>
<div class="field">
<span class="caption">format as a list: the first writes, each one reads</span>
<DatePicker aria-label="Several formats" format={['D MMM YYYY', 'DD/MM/YYYY', 'YYYY-MM-DD']} value={today} />
</div>
<div class="field">
<span class="caption">format as a function</span>
<DatePicker aria-label="Long date" format={long} value={today} inputReadOnly />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
showTime: the value waits for OK Value: nothing
12 hours, steps of 15 minutes, no seconds
needConfirm=false, showNow=false Value: nothing
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
let moment = $state<Date | null>(null);
let instant = $state<Date | null>(null);
const nine = new Date(2026, 0, 1, 9, 0);
</script>
<div class="stack">
<div class="field">
<span class="caption">showTime: the value waits for OK</span>
<DatePicker aria-label="Date and time" showTime bind:value={moment} />
<span role="status">Value: {moment ? moment.toString().slice(0, 24) : 'nothing'}</span>
</div>
<div class="field">
<span class="caption">12 hours, steps of 15 minutes, no seconds</span>
<DatePicker aria-label="Meeting" showTime={{ use12Hours: true, minuteStep: 15, showSecond: false, defaultOpenValue: nine }} />
</div>
<div class="field">
<span class="caption">needConfirm=false, showNow=false</span>
<DatePicker
aria-label="Taken at once"
showTime={{ format: 'HH:mm:ss', hourStep: 2, secondStep: 30 }}
needConfirm={false}
showNow={false}
bind:value={instant}
/>
<span role="status">Value: {instant ? instant.toString().slice(0, 24) : 'nothing'}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
disabledDate: working days only
minDate and maxDate: the rest of this month
disabledTime: 9:00 to 17:59
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
const today = new Date();
const monthEnd = new Date(today.getFullYear(), today.getMonth() + 1, 0);
const weekend = (date: Date) => date.getDay() === 0 || date.getDay() === 6;
const officeHours = () => ({
disabledHours: () => Array.from({ length: 24 }, (_, hour) => hour).filter((hour) => hour < 9 || hour > 17)
});
</script>
<div class="stack">
<div class="field">
<span class="caption">disabledDate: working days only</span>
<DatePicker aria-label="Working day" disabledDate={(date, { type }) => type === 'date' && weekend(date)} />
</div>
<div class="field">
<span class="caption">minDate and maxDate: the rest of this month</span>
<DatePicker aria-label="This month" minDate={today} maxDate={monthEnd} />
</div>
<div class="field">
<span class="caption">disabledTime: 9:00 to 17:59</span>
<DatePicker
aria-label="Office hours"
showTime={{ format: 'HH:mm', hideDisabledOptions: true }}
disabledTime={officeHours}
/>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
needConfirm, onok, extraFooter
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
let value = $state<Date | null>(null);
let confirmed = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">needConfirm, onok, extraFooter</span>
<DatePicker
aria-label="Delivery day"
needConfirm
extraFooter="Deliveries take two working days"
bind:value
onok={(date) => (confirmed = date.toDateString())}
/>
{#if confirmed}<span role="status">Confirmed: {confirmed}</span>{/if}
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
const inDays = (days: number) => () => {
const today = new Date();
return new Date(today.getFullYear(), today.getMonth(), today.getDate() + days);
};
const presets = [
{ label: 'Tomorrow', value: inDays(1) },
{ label: 'In a week', value: inDays(7) },
{ label: 'In a month', value: inDays(30) }
];
let date = $state<Date | null>(null);
</script>
<div class="stack">
<div class="field">
<DatePicker aria-label="Due date" {presets} bind:value={date} />
<span role="status">Due: {date ? date.toDateString() : 'not set'}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(260px, 100%);
}
</style>
multiple, order=true 2 chosen
2026-01-05 2026-01-07 2026-01-05, 2026-01-07
maxTagCount=2, maxTagPlaceholder
2026-01-05 2026-01-07 and 3 more 2026-01-05, 2026-01-07, 2026-01-09, 2026-01-11, 2026-01-13
maxTagCount="responsive": one row
2026-01-05 2026-01-07 2026-01-09 2026-01-11 2026-01-13 2026-01-15 2026-01-17 2026-01-19 + 1 ... 2026-01-05, 2026-01-07, 2026-01-09, 2026-01-11, 2026-01-13, 2026-01-15, 2026-01-17, 2026-01-19
tagRender
5 Jan, 7 Jan, 9 Jan
<script lang="ts">
import { DatePicker, Switch } from 'svelte-ant-design';
const days = (count: number) => Array.from({ length: count }, (_, index) => new Date(2026, 0, 5 + index * 2));
let dates = $state<Date[]>(days(2));
let sorted = $state(true);
</script>
<div class="stack">
<div class="field">
<span class="caption">multiple, order={sorted}</span>
<label class="row"><Switch bind:checked={sorted} aria-label="Keep the dates sorted" /> order</label>
<DatePicker aria-label="Training days" multiple order={sorted} bind:value={dates} />
<span role="status">{dates.length} chosen</span>
</div>
<div class="field">
<span class="caption">maxTagCount=2, maxTagPlaceholder</span>
<DatePicker aria-label="Two tags" multiple maxTagCount={2} value={days(5)}>
{#snippet maxTagPlaceholder(omitted)}and {omitted.length} more{/snippet}
</DatePicker>
</div>
<div class="field">
<span class="caption">maxTagCount="responsive": one row</span>
<DatePicker aria-label="One row" multiple maxTagCount="responsive" value={days(8)} />
</div>
<div class="field">
<span class="caption">tagRender</span>
<DatePicker aria-label="Own tags" multiple format="D MMM" value={days(3)}>
{#snippet tagRender({ label, closable, onclose })}
<button type="button" class="chip" disabled={!closable} onclick={onclose} aria-label="Remove {label}">{label} ×</button>
{/snippet}
</DatePicker>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(300px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
align-items: center;
gap: 8px;
}
.chip {
margin-inline-end: 4px;
padding: 0 6px;
border: 1px solid var(--ads-color-primary);
border-radius: 10px;
background: none;
color: var(--ads-color-text);
font: inherit;
font-size: 12px;
cursor: pointer;
}
</style>
<script lang="ts">
import { DateRangePicker, type DateRange } from 'svelte-ant-design';
let stay = $state<DateRange | null>(null);
let last = $state('');
const day = (date: Date | null) => (date ? date.toDateString() : 'empty');
</script>
<div class="stack">
<div class="field">
<DateRangePicker
aria-label="Stay"
bind:value={stay}
oncalendarchange={(value, text, { range }) => (last = `${range}: ${text[range === 'start' ? 0 : 1]}`)}
/>
<span role="status">{stay ? `${day(stay[0])} – ${day(stay[1])}` : 'No stay chosen'}</span>
{#if last}<span role="status">Last end chosen, {last}</span>{/if}
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(360px, 100%);
}
</style>
picker="month"
showTime: each end waits for OK
presets
<script lang="ts">
import { DateRangePicker, type DateRange } from 'svelte-ant-design';
let shift = $state<DateRange | null>(null);
let confirmed = $state('');
const lastDays = (days: number) => (): [Date, Date] => {
const end = new Date();
return [new Date(end.getFullYear(), end.getMonth(), end.getDate() - days + 1), end];
};
const presets = [
{ label: 'Last 7 days', value: lastDays(7) },
{ label: 'Last 30 days', value: lastDays(30) }
];
</script>
<div class="stack">
<div class="field">
<span class="caption">picker="month"</span>
<DateRangePicker aria-label="Months of the report" picker="month" />
</div>
<div class="field">
<span class="caption">showTime: each end waits for OK</span>
<DateRangePicker
aria-label="Shift"
showTime={{ format: 'HH:mm' }}
bind:value={shift}
onok={(value) => (confirmed = `${value[0]?.getHours()}:00 to ${value[1]?.getHours()}:00`)}
/>
{#if confirmed}<span role="status">Hours confirmed: {confirmed}</span>{/if}
</div>
<div class="field">
<span class="caption">presets</span>
<DateRangePicker aria-label="Period" {presets} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(380px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
allowEmpty: the end may stay empty
disabled={[true, false]}: the start is fixed
2026-01-12 2026-01-16
disabledDate with from: seven days at most
order=false, separator, id
<script lang="ts">
import { DateRangePicker } from 'svelte-ant-design';
const booked: [Date, Date] = [new Date(2026, 0, 12), new Date(2026, 0, 16)];
const dayLength = 24 * 60 * 60 * 1000;
// Seven days at most, counting both ends: once one end is chosen, the other is six days from it or nearer. The days are
// rounded, because a day on which the clocks change is not 24 hours long.
const withinWeek = (date: Date, { from }: { from?: Date }) =>
from !== undefined && Math.round(Math.abs(date.getTime() - from.getTime()) / dayLength) > 6;
let changed = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">allowEmpty: the end may stay empty</span>
<DateRangePicker
aria-label="Contract"
allowEmpty={[false, true]}
placeholder={['Signed', 'Ends (optional)']}
inputLabels={['Date of signing', 'Date of the end']}
onchange={(value, text) => (changed = text.map((part) => part || 'open').join(' – '))}
/>
{#if changed}<span role="status">{changed}</span>{/if}
</div>
<div class="field">
<span class="caption">{'disabled={[true, false]}'}: the start is fixed</span>
<DateRangePicker aria-label="Extension" disabled={[true, false]} value={booked} />
</div>
<div class="field">
<span class="caption">disabledDate with from: seven days at most</span>
<DateRangePicker aria-label="Short trip" disabledDate={withinWeek} />
</div>
<div class="field">
<span class="caption">order=false, separator, id</span>
<DateRangePicker aria-label="Night shift" order={false} separator="to" id={{ start: 'shift-from', end: 'shift-to' }} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(360px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
disabled
2026-12-02
inputReadOnly: no typing, no keyboard on a phone
allowClear=false
2026-12-02
allowClear with an icon and a name
2026-12-02
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
const today = new Date();
</script>
<div class="stack">
<div class="field">
<span class="caption">disabled</span>
<DatePicker aria-label="Disabled" disabled value={today} />
</div>
<div class="field">
<span class="caption">inputReadOnly: no typing, no keyboard on a phone</span>
<DatePicker aria-label="Read-only input" inputReadOnly placeholder="Pick on the calendar" />
</div>
<div class="field">
<span class="caption">allowClear=false</span>
<DatePicker aria-label="No clear button" allowClear={false} value={today} />
</div>
<div class="field">
<span class="caption">allowClear with an icon and a name</span>
<DatePicker aria-label="Own clear button" allowClear={{ clearIcon: '×', label: 'Forget the date' }} value={today} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
size="large"
size="medium"
size="small"
<script lang="ts">
import { DatePicker, DateRangePicker } from 'svelte-ant-design';
const sizes = ['large', 'medium', 'small'] as const;
</script>
<div class="stack">
{#each sizes as size (size)}
<div class="field">
<span class="caption">size="{size}"</span>
<DatePicker aria-label="{size} date" {size} />
<DateRangePicker aria-label="{size} range" {size} />
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(340px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
variant="outlined"
variant="filled"
variant="borderless"
variant="underlined"
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
const variants = ['outlined', 'filled', 'borderless', 'underlined'] as const;
</script>
<div class="stack">
{#each variants as variant (variant)}
<div class="field">
<span class="caption">variant="{variant}"</span>
<DatePicker aria-label="{variant} date" {variant} />
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(260px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
status="error" Choose your birthday.
status="warning" These days are almost fully booked.
<script lang="ts">
import { DatePicker, DateRangePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">status="error"</span>
<DatePicker aria-label="Birthday" status="error" />
<span class="hint">Choose your birthday.</span>
</div>
<div class="field">
<span class="caption">status="warning"</span>
<DateRangePicker aria-label="Holiday" status="warning" />
<span class="hint">These days are almost fully booked.</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(340px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.hint {
color: var(--ads-color-text-secondary);
}
</style>
prefix and suffix
From ✈
suffix=null: no icon
prevIcon, nextIcon, superPrevIcon, superNextIcon
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">prefix and suffix</span>
<DatePicker aria-label="Flight" prefix="From" suffix="✈" />
</div>
<div class="field">
<span class="caption">suffix=null: no icon</span>
<DatePicker aria-label="No icon" suffix={null} />
</div>
<div class="field">
<span class="caption">prevIcon, nextIcon, superPrevIcon, superNextIcon</span>
<DatePicker aria-label="Own arrows">
{#snippet prevIcon()}‹{/snippet}
{#snippet nextIcon()}›{/snippet}
{#snippet superPrevIcon()}«{/snippet}
{#snippet superNextIcon()}»{/snippet}
</DatePicker>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
cellRender: the paydays are underlined
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
const payday = (date: Date) => date.getDate() === 5 || date.getDate() === 20;
</script>
<div class="stack">
<div class="field">
<span class="caption">cellRender: the paydays are underlined</span>
<DatePicker aria-label="Payday">
{#snippet cellRender({ date, type, inView, text })}
<span class:payday={type === 'date' && inView && payday(date)}>{text}</span>
{/snippet}
</DatePicker>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.payday {
text-decoration: underline 2px var(--ads-color-primary);
text-underline-offset: 3px;
}
</style>
open and pickerValue held by the page Opened 0 times
showWeek and placement="bottomRight"
mode="year": the panel stays on the years
container: the panel is drawn inside the box
<script lang="ts">
import { Button, DatePicker, type PanelMode } from 'svelte-ant-design';
let open = $state(false);
let shownPeriod = $state(new Date(2026, 5, 1));
let moved = $state('');
let opened = $state(0);
let box = $state<HTMLDivElement>();
const describe = (value: Date, mode: PanelMode) => `${mode} panel, ${value.getFullYear()}-${value.getMonth() + 1}`;
</script>
<div class="stack">
<div class="field">
<span class="caption">open and pickerValue held by the page</span>
<DatePicker
aria-label="Controlled panel"
bind:open
bind:pickerValue={shownPeriod}
onpanelchange={(value, mode) => (moved = describe(value, mode))}
onopenchange={(next) => next && opened++}
/>
<Button onclick={() => (open = true)}>Open the panel</Button>
<span role="status">Opened {opened} times{moved ? `, moved to the ${moved}` : ''}</span>
</div>
<div class="field">
<span class="caption">showWeek and placement="bottomRight"</span>
<DatePicker aria-label="With week numbers" showWeek placement="bottomRight" />
</div>
<div class="field">
<span class="caption">mode="year": the panel stays on the years</span>
<DatePicker aria-label="Years only panel" mode="year" />
</div>
<div class="field">
<span class="caption">container: the panel is drawn inside the box</span>
<div class="box" bind:this={box}>
<DatePicker aria-label="Inside the box" container={() => box ?? document.body} />
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
position: relative;
justify-self: stretch;
padding: 12px;
border: 1px dashed var(--ads-color-border);
}
</style>
locale={ru}
2 декабря 2026
locale={zh}
<script lang="ts">
import { DatePicker, DateRangePicker } from 'svelte-ant-design';
import ru from 'svelte-ant-design/locales/ru';
import zh from 'svelte-ant-design/locales/zh-cn';
const today = new Date();
</script>
<div class="stack">
<div class="field" lang="ru">
<span class="caption">{'locale={ru}'}</span>
<DatePicker aria-label="Дата" locale={ru} format="D MMMM YYYY" value={today} />
<DateRangePicker aria-label="Период" locale={ru} />
</div>
<div class="field" lang="zh-CN">
<span class="caption">{'locale={zh}'}</span>
<DatePicker aria-label="日期" locale={zh} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(340px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
let log = $state<string[]>([]);
const note = (line: string) => (log = [line, ...log].slice(0, 4));
</script>
<div class="stack">
<div class="field">
<DatePicker
aria-label="Watched date"
onchange={(_, text) => note(`onchange: ${text || 'empty'}`)}
onclear={() => note('onclear')}
onfocus={() => note('onfocus')}
onblur={() => note('onblur')}
/>
<ul class="log" aria-live="polite">
{#each log as line, index (index)}<li>{line}</li>{/each}
</ul>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.log {
min-height: 5.5em;
margin: 0;
padding-inline-start: 18px;
}
</style>
Check-in
<script lang="ts">
import { Button, DatePicker, Select, SpaceCompact } from 'svelte-ant-design';
let kind = $state('Check-in');
</script>
<div class="stack">
<div class="field">
<SpaceCompact block>
<Select aria-label="Kind of date" bind:value={kind} options={['Check-in', 'Check-out']} style="width: 150px" />
<DatePicker aria-label="Date of the visit" />
<Button type="primary">Search</Button>
</SpaceCompact>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(420px, 100%);
}
</style>
classNames and styles
<script lang="ts">
import { DatePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">classNames and styles</span>
<DatePicker
aria-label="Styled date"
class="rounded"
style="max-width: 240px"
classNames={{ popup: 'soft', input: 'strong' }}
styles={{ root: { 'border-radius': '16px' }, suffix: { color: 'var(--ads-color-primary)' } }}
/>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(280px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
:global(.rounded .strong) {
font-weight: 600;
}
:global(.soft .ads-picker-header) {
background: rgba(22, 119, 255, 0.08);
}
</style>
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 所选日期;multiple 时为日期数组。可用 bind:value 绑定。 | Date | null; Date[] | — |
| picker | 选择的单位。周、月、季度和年以其第一天表示。 | 'date' | 'week' | 'month' | 'quarter' | 'year' | 'date' |
| multiple | 多选日期,每个显示为标签;value、onchange、onok 的类型随之变化。不能与 showTime 同用。 | boolean | false |
| format | 字段中的书写方式;列表时第一个用于显示,任一个可用于解析。默认 YYYY-MM-DD,周为 gggg-wo,以及 YYYY-MM、YYYY-[Q]Q、YYYY。 | string | string[] | (date: Date) => string | — |
| showTime | 在日期旁选择时间(仅 picker="date")。 | boolean | TimeOptions | false |
| disabledDate | 不可选的日期;type 表示单元格代表的单位。 | (date: Date, info: { type }) => boolean | — |
| disabledTime | 给定日期下不可选的时、分、秒。 | (date: Date | null) => DisabledTimes | — |
| minDate | 最早可选日期;面板不会翻到它之前。 | Date | — |
| maxDate | 最晚可选日期。 | Date | — |
| needConfirm | 点击确定后才改变值;showTime 时默认开启。 | boolean | — |
| showNow | 日期选择的“今天”按钮,带时间时为“此刻”。 | boolean | true |
| showWeek | 显示周序号列(picker="week" 时始终显示)。 | boolean | false |
| presets | 面板旁的预设项。 | { label: string | Snippet; value: Date | (() => Date) }[] | [] |
| mode | 当前显示的面板,由父组件控制。 | 'date' | 'month' | 'quarter' | 'year' | 'decade' | — |
| pickerValue | 面板显示的时间段。可用 bind:pickerValue 绑定。 | Date | — |
| placeholder | 空字段的占位提示;默认取自语言包。 | string | — |
| disabled | 禁用字段。 | boolean | false |
| inputReadOnly | 字段不可输入;手机不弹出键盘。 | boolean | false |
| allowClear | 清除按钮;传对象可改图标、名称和 tabindex。 | boolean | { clearIcon?, label?, tabIndex? } | true |
| size | 默认 medium(紧凑布局可覆盖);middle、default 为别名。 | 'large' | 'medium' | 'middle' | 'small' | 'default' | — |
| variant | 字段外观。 | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | 标示错误或警告;error 同时设置 aria-invalid。 | 'error' | 'warning' | — |
| prefix | 文本之前的内容。 | string | Snippet | — |
| suffix | 替换日历图标;null 隐藏。 | string | Snippet | null | — |
| prevIcon | 上一月箭头。 | Snippet | — |
| nextIcon | 下一月箭头。 | Snippet | — |
| superPrevIcon | 向前一年(或十年)箭头。 | Snippet | — |
| superNextIcon | 向后一年(或十年)箭头。 | Snippet | — |
| cellRender | 单元格内容:{ date, type, today, selected, inView, text }。 | Snippet<[cell: DateCell]> | — |
| extraFooter | 面板底部内容;片段会得到当前面板类型。 | string | Snippet<[mode]> | — |
| tagRender | multiple 的标签:{ value, label, closable, onclose }。 | Snippet<[tag: DatePickerTag]> | — |
| maxTagCount | multiple 时最多显示的标签数;responsive 单行尽量多显示。 | number | 'responsive' | — |
| maxTagPlaceholder | 折叠标签的内容。 | string | Snippet<[omitted: Date[]]> | — |
| order | multiple 时日期按先后排序。 | boolean | true |
| open | 面板是否打开。可用 bind:open 绑定。 | boolean | false |
| placement | 面板的首选位置;靠近屏幕边缘时翻转。 | 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight' | 'bottomLeft' |
| container | 面板挂载位置(默认 <body> 末尾)。 | HTMLElement | (anchor) => HTMLElement | — |
| locale | 名称、文字与每周首日:import ru from 'svelte-ant-design/locales/ru'。 | DatePickerLocale | en |
| onchange | 用户改变了值;第二个参数为文本。 | (value, text) => void | — |
| onopenchange | 面板打开或关闭。 | (open: boolean) => void | — |
| onpanelchange | 面板切换了时间段或类型。 | (value: Date, mode) => void | — |
| onok | needConfirm 时通过确定或回车确认了值。 | (value) => void | — |
| onclear | 点击清除按钮清空了值。 | () => void | — |
| onfocus | 焦点从外部进入字段或面板。 | (event: FocusEvent) => void | — |
| onblur | 焦点离开字段与面板。 | (event: FocusEvent) => void | — |
| classNames | root、input、prefix、suffix、popup、header、body、cell、footer 的附加类名。 | DatePickerClassNames | — |
| styles | 同名部分的内联样式。 | DatePickerStyles | — |
| class / style | 字段的类名与样式。 | string | — |
| native attributes | 其他属性传给 <input>(id、name、aria-*、autofocus)。 | HTMLInputAttributes | — |
与 Ant Design 的差异:值为原生
Date 而非 dayjs;RangePicker 对应 DateRangePicker;语言包来自 svelte-ant-design/locales;键盘操作遵循 ARIA 日期选择模式。本版本暂不支持:掩码 format、previewValue、panelRender、自定义底部按钮以及替换日期库。DateRangePicker
DateRangePicker 支持 DatePicker 除 multiple、tagRender、maxTagCount、maxTagPlaceholder 外的属性;下列属性有所不同或为其独有。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 开始与结束;未选的一端为 null。可用 bind:value 绑定。 | [Date | null, Date | null] | null | null |
| placeholder | 两个输入框的占位提示;默认取自语言包。 | [string, string] | — |
| inputLabels | 两个输入框的无障碍名称;默认取自语言包。 | [string, string] | — |
| disabled | 整体或分别禁用。禁用端保留值并限制先后顺序。 | boolean | [boolean, boolean] | false |
| allowEmpty | 允许留空的一端。 | [boolean, boolean] | [false, false] |
| separator | 两个输入框之间的内容;默认箭头。 | string | Snippet | — |
| order | 开始不晚于结束:本次选择的两端会交换,之前保留的一端会重新选择。 | boolean | true |
| id | 两个输入框的 id。 | { start?: string; end?: string } | — |
| autofocus | 页面加载时聚焦开始输入框。 | boolean | false |
| disabledDate | 不可选日期;from 为已选的另一端。 | (date, info: { type, from? }) => boolean | — |
| disabledTime | 当前选择端不可选的时间。 | (date: Date | null, range: 'start' | 'end') => DisabledTimes | — |
| presets | 同时给出两端的预设项。 | { label; value: [Date, Date] | (() => [Date, Date]) }[] | [] |
| onchange | 范围改变:两端完整后调用一次。 | (value, text: [string, string]) => void | — |
| oncalendarchange | 选择了某一端;range 指明是哪端。 | (value, text, info: { range }) => void | — |
| onok | needConfirm 时确认了范围。 | (value) => void | — |
| onfocus | 焦点从外部进入字段或面板。 | (event, info: { range }) => void | — |
| onblur | 焦点离开字段与面板。 | (event, info: { range }) => void | — |
| native attributes | 其他属性传给根元素(group):aria-label、data-*。 | HTMLAttributes<HTMLDivElement> | — |
TimeOptions
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| format | 时间模板;决定显示哪些列。use12Hours 时默认为 hh:mm:ss a;不显示秒时省略。 | string | 'HH:mm:ss' |
| hourStep | 小时步长。 | number | 1 |
| minuteStep | 分钟步长。 | number | 1 |
| secondStep | 秒步长。 | number | 1 |
| use12Hours | 12 小时制,含 AM/PM。 | boolean | false |
| showSecond | false 隐藏秒。 | boolean | true |
| defaultOpenValue | 选择时的初始时间(和日期)。 | Date | — |
| hideDisabledOptions | 隐藏禁用的数值。 | boolean | false |
DatePickerLocale
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| calendar | 月份、星期名称与周规则,格式同 dayjs 语言包;缺省为英语。 | CalendarData | — |
| texts | 组件文字:占位符、按钮与面板名称、“今天”、“确定”,以及多选时选中或移除日期后播报的词(announce,缺省为英文)。 | DatePickerTexts | — |
| patterns | 面板中月份、年份、星期与日期的写法。 | DatePickerPatterns | — |
方法
| 方法 | 说明 | 类型 |
|---|---|---|
| focus | 让字段获得焦点;范围选择时聚焦当前端。不会打开面板。 | (options?: FocusOptions) => void |
| blur | 让字段失焦;面板随之关闭。 | () => void |
设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-picker-panel-width | 单个日历的宽度。 | 288px |
| --ads-picker-header-height | 面板头部高度。 | 40px |
| --ads-picker-cell-height | 日期单元格高度。 | 36px |
| --ads-picker-cell-mark | 单元格标记高度。 | 24px |
| --ads-picker-period-height | 月、季度、年单元格高度。 | 66px |
| --ads-picker-actions-height | 面板底部按钮区的最小高度。 | 42px |
| --ads-picker-presets-width | 预设列宽度。 | 120px |
| --ads-picker-popup-radius | 面板圆角半径。 | 8px |
| --ads-picker-popup-bg | 面板背景。 | #ffffff |
| --ads-picker-popup-shadow | 面板阴影。 | 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-picker-icon-color | 字段与头部图标颜色。 | rgba(0, 0, 0, 0.45) |
| --ads-picker-week-color | 周序号颜色。 | var(--ads-color-text-disabled) |
| --ads-picker-outside-color | 相邻月份日期颜色。 | var(--ads-color-text-disabled) |
| --ads-picker-cell-hover-bg | 悬停单元格背景。 | rgba(0, 0, 0, 0.04) |
| --ads-picker-disabled-bg | 禁用单元格背景。 | rgba(0, 0, 0, 0.04) |
| --ads-picker-today-border | 今天与悬停范围端点的边框色。 | var(--ads-color-primary) |
| --ads-picker-selected-bg | 选中日期背景。 | var(--ads-color-primary-solid) |
| --ads-picker-selected-color | 选中日期文字颜色。 | #ffffff |
| --ads-picker-time-column-width | 时间列宽度。 | 56px |
| --ads-picker-time-column-height | 时间列高度。 | 262px |
| --ads-picker-time-item-height | 时间项高度。 | 28px |
| --ads-picker-time-selected-bg | 选中时间项背景。 | #e6f4ff |
| --ads-picker-range-bg | 范围内日期背景。 | #e6f4ff |
| --ads-picker-range-hover-bg | 悬停时预览范围背景。 | #f0f8ff |
键盘操作
- 在字段中按
↓打开面板并将焦点移入日历。 - 日历中方向键按天和周移动,
Home、End到本周首尾,Page Up、Page Down按月翻页,加Shift按年。 Enter或空格确认选择。范围选择时焦点移到另一端输入框,两端选完后回到输入框。Escape关闭面板并将焦点返回字段。Tab离开字段并关闭面板;范围选择时先从开始移到结束。面板内依次经过头部按钮、日历(一个焦点)与底部。- 时间列中
↑、↓改变数值,←、→切换列,Enter确认。
无障碍
- 字段是关联对话框的
combobox;日历是以月份命名的网格,每一天用完整日期命名,今天标记为当前项。切换月份时只播报一次。 - 范围选择是包含两个具名输入框的
group;对话框说明正在选择哪一端。请用aria-label为该组命名。 - 默认浅色配色沿用 Ant Design,部分对比度未达 4.5:1,可引入
tokens-accessible.css。今天、所选日期与范围的标记在强制颜色模式下依然可见。 - 已用真实屏幕阅读器(Orca 搭配 Firefox)检查:朗读字段及其值、对话框名称、日历日期及其状态、月份、多选中被选择和移除的日期、范围两端和时间列。Orca 自己不会朗读此类字段的值,因此每个输入框都把值作为描述提供:未检查的 NVDA、JAWS、VoiceOver 可能会读两遍。未用 NVDA、JAWS、VoiceOver 或手机检查。