DatePicker
Select a year, month, date, date and time, or a date range in the panel. You can also type a value using the display format.
Import
import { HuiDatePicker } from '@nscode/handy-ui'
Native attributes and labels
class and style remain on the visible wrapper. id, name, autocomplete, tabindex, ARIA attributes and other native attributes are applied once to the input. Explicit IDs update the FormItem label target; user descriptions are merged with the form hint and error IDs without duplicates. The reserved data-hui-field, data-invalid, data-disabled, data-open and data-filled markers are applied only to the element that draws the border, and a native value attribute does not replace the actual model.
When editable=false, there is no native input: attributes target the element with role="button". Use aria-label or aria-labelledby to name it. name on this trigger does not create a native form field.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiDatePicker } from '@nscode/handy-ui'
const version = ref(1)
const value = ref<Date | null>(new Date(2026, 10, 20))
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="version++">Change field ID</HuiButton>
<label :for="`native-datepicker-${version}`">Field label</label>
<HuiDatePicker
:id="`native-datepicker-${version}`"
v-model="value"
name="native-datepicker"
aria-describedby="native-datepicker-help"
autocomplete="on"
style="width: 280px; max-width: 100%"
/>
<span id="native-datepicker-help">The label and description follow the input ID.</span>
</HuiSpace>
</template>
Basic usage
Bind with v-model. Without valueFormat, selections emit Date; an empty value is null.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiDatePicker v-model="value" style="width: 240px" />
<p style="margin-top: 12px">Value: {{ value ? value.toLocaleString() : 'null' }}</p>
</template>
Picker types
type determines the panel and the selected value:
| type | Panel | Selected value |
|---|---|---|
year | Years | January 1 of that year, at local midnight |
month | Months | First day of that month, at local midnight |
date | Dates (default) | Selected day, at local midnight |
datetime | Dates with time inputs | Date with hours, minutes and seconds |
daterange | Two month panels | [start, end], both at local midnight |
datetimerange | Two panels with time inputs | [start, end], each with its own time |
Each row uses a different type. The selected value appears on the right:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const year = ref<Date | null>(null)
const month = ref<Date | null>(null)
const date = ref<Date | null>(null)
const datetime = ref<Date | null>(null)
const daterange = ref<[Date, Date] | null>(null)
const datetimerange = ref<[Date, Date] | null>(null)
// Display Date in local time; join range endpoints with ~.
const show = (value: Date | [Date, Date] | null) => {
if (!value) return 'null'
const text = (date: Date) => date.toLocaleString()
return Array.isArray(value) ? value.map(text).join(' ~ ') : text(value)
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">year</span>
<HuiDatePicker v-model="year" type="year" style="width: 240px" />
<span class="row-value">{{ show(year) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">month</span>
<HuiDatePicker v-model="month" type="month" style="width: 240px" />
<span class="row-value">{{ show(month) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">date</span>
<HuiDatePicker v-model="date" type="date" style="width: 240px" />
<span class="row-value">{{ show(date) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">datetime</span>
<HuiDatePicker v-model="datetime" type="datetime" style="width: 240px" />
<span class="row-value">{{ show(datetime) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">daterange</span>
<HuiDatePicker v-model="daterange" type="daterange" style="width: 300px" />
<span class="row-value">{{ show(daterange) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">datetimerange</span>
<HuiDatePicker v-model="datetimerange" type="datetimerange" style="width: 400px" />
<span class="row-value">{{ show(datetimerange) }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 104px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row-value {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Date ranges
With type="daterange", click two dates to select a range. Either endpoint can be selected first; the emitted endpoints are sorted. Closing after only one click discards that unfinished selection. After selecting the first date, hovering over another date or moving there with arrow keys previews the range.
A range uses one input with a start placeholder, separator and end placeholder. Defaults come from the locale. A single placeholder replaces the whole placeholder; empty placeholders retain the locale fallback, while an explicitly empty separator stays empty. Weekdays, months, buttons and accessible names also follow the locale. Bound date values and format do not change with locale.
By default, the panels show adjacent months and navigate together. Set unlinkPanels to navigate each panel independently.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const range1 = ref<[Date, Date] | null>(null)
const range2 = ref<[Date, Date] | null>(null)
const range3 = ref<[Date, Date] | null>(null)
const range4 = ref<[Date, Date] | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Default</span>
<HuiDatePicker v-model="range1" type="daterange" style="width: 300px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Start, end, separator</span>
<HuiDatePicker
v-model="range2"
type="daterange"
start-placeholder="Check-in"
end-placeholder="Check-out"
range-separator="to"
style="width: 300px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">One placeholder</span>
<HuiDatePicker
v-model="range3"
type="daterange"
placeholder="Select a reporting period"
style="width: 300px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">unlink-panels</span>
<HuiDatePicker v-model="range4" type="daterange" unlink-panels style="width: 300px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 120px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Dates with time
The datetime and datetimerange panels stay open after date selection. Editing an hour, minute or second applies immediately. Values are clamped to 0–23 or 0–59; clearing an input restores its previous value. You can set the time before selecting a date. Within a range on the same day, the start cannot be later than the end.
The footer button is Now for datetime and Today for date (local midnight). Range types have no such button.
defaultTime supplies the time used when selecting a date. Pass one Date for datetime; only its time fields are used. Pass two for datetimerange, often midnight for the start and 23:59:59 for the end:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const range = ref<[Date, Date] | null>(null)
// Only the time fields matter; the calendar date is arbitrary.
const nineOClock = new Date(2000, 0, 1, 9, 0, 0)
const defaultTime: [Date, Date] = [
new Date(2000, 0, 1, 0, 0, 0),
new Date(2000, 0, 1, 23, 59, 59)
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Default 09:00:00</span>
<HuiDatePicker
v-model="value"
type="datetime"
:default-time="nineOClock"
style="width: 240px"
/>
<span class="row-value">{{ value ? value.toLocaleString() : 'null' }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Start 00:00, end 23:59:59</span>
<HuiDatePicker
v-model="range"
type="datetimerange"
:default-time="defaultTime"
style="width: 400px"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 160px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row-value {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Formats
formatcontrols the input display and parsing of typed text. Defaults depend on the type:YYYY,YYYY-MM,YYYY-MM-DDorYYYY-MM-DD HH:mm:ss.- Without
valueFormat, the bound value isDate.xemits a millisecond timestamp number,Xemits a second timestamp number, and other formats emit strings. Bound values are parsed using that format. Numeric input uses seconds only withX, and milliseconds otherwise.
| Token | Meaning | Token | Meaning |
|---|---|---|---|
YYYY | Four-digit year | HH | Two-digit hour, 24-hour clock |
YY | Two-digit year | H | Hour without padding |
MM | Two-digit month | mm | Two-digit minute |
M | Month without padding | m | Minute without padding |
DD | Two-digit day | ss | Two-digit second |
D | Day without padding | s | Second without padding |
Z | Local offset ±HH:mm | ZZ | Local offset ±HHmm |
Square brackets preserve literal text, for example YYYY[ year ]M[ month ]D[ day].
Z / ZZ parse explicit offsets, and input Z means UTC. Output uses local fields and the local offset at that instant; UTC environments emit +00:00 / +0000. Input offsets are not retained. [Z] / [ZZ] remain literals. Offset hours range from 00 to 23 and minutes from 00 to 59. Typed and bound strings with an offset format must match strictly: invalid dates, invalid or missing offsets, and Z/ZZ shape mismatches do not fall back to loose parsing.
x and X must be the entire format; they cannot be combined with other tokens. The x in [x] or YYYY[x] remains literal text. Seconds are rounded down: -1 millisecond becomes -1 second with X, and reading it back has second precision.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
const value = ref('2024-01-15')
</script>
<template>
<HuiDatePicker
v-model="value"
format="YYYY[ year ]M[ month ]D[ day]"
value-format="YYYY-MM-DD"
style="width: 240px"
/>
</template>
Display format
These pickers share one value but use different display formats. Changing a date in one updates the others.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date(2024, 0, 5, 9, 8, 7))
const range = ref<[Date, Date] | null>([new Date(2024, 0, 5), new Date(2024, 0, 12)])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">YYYY/MM/DD</span>
<HuiDatePicker v-model="value" format="YYYY/MM/DD" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY[ year ]M[ month ]D[ day]</span>
<HuiDatePicker v-model="value" format="YYYY[ year ]M[ month ]D[ day]" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">DD.MM.YY</span>
<HuiDatePicker v-model="value" format="DD.MM.YY" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY-MM-DD HH:mm</span>
<HuiDatePicker
v-model="value"
type="datetime"
format="YYYY-MM-DD HH:mm"
style="width: 240px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YY-M-D H:m</span>
<HuiDatePicker v-model="value" type="datetime" format="YY-M-D H:m" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY[ year ]MM[ month]</span>
<HuiDatePicker
v-model="value"
type="month"
format="YYYY[ year ]MM[ month]"
style="width: 240px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY[ fiscal year]</span>
<HuiDatePicker v-model="value" type="year" format="YYYY[ fiscal year]" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Range YYYY/M/D</span>
<HuiDatePicker v-model="range" type="daterange" format="YYYY/M/D" style="width: 300px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 144px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Month and year selections normalize to the first day of the month or year at midnight, so selecting them also changes the day and time shown by the other pickers.
Bound value format
valueFormat determines what v-model receives. The current bound value appears on the right:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const asDate = ref<Date | null>(new Date(2024, 0, 15))
const asString = ref('2024-01-15')
const compact = ref('20240115')
const withTime = ref('2024/01/15 09:30:00')
const range = ref<[string, string] | null>(['2024-01-15', '2024-01-20'])
const show = (value: unknown) => {
if (value instanceof Date) return `Date:${value.toLocaleString()}`
return JSON.stringify(value)
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Unset</span>
<HuiDatePicker v-model="asDate" style="width: 240px" />
<span class="row-value">{{ show(asDate) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY-MM-DD</span>
<HuiDatePicker v-model="asString" value-format="YYYY-MM-DD" style="width: 240px" />
<span class="row-value">{{ show(asString) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYYMMDD</span>
<HuiDatePicker v-model="compact" value-format="YYYYMMDD" style="width: 240px" />
<span class="row-value">{{ show(compact) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">YYYY/MM/DD HH:mm:ss</span>
<HuiDatePicker
v-model="withTime"
type="datetime"
value-format="YYYY/MM/DD HH:mm:ss"
style="width: 240px"
/>
<span class="row-value">{{ show(withTime) }}</span>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Range YYYY-MM-DD</span>
<HuiDatePicker
v-model="range"
type="daterange"
value-format="YYYY-MM-DD"
style="width: 300px"
/>
<span class="row-value">{{ show(range) }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 144px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row-value {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Date and time with offsets
The two initial values below use positive +05:45 and negative -0330 offsets to identify the same instant. Inputs display local time; selecting or typing a new value emits the local offset. You can type UTC input ending in Z; [Z] is literal text rather than a UTC marker. This feature handles numeric offsets, without an IANA timezone option or retention of the original offset.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerValue } from '@nscode/handy-ui'
const colon = ref<DatePickerValue>('2024-01-01T00:15:00+05:45')
const compact = ref<DatePickerValue>('2023-12-31T15:00:00-0330')
const resetUtc = () => {
colon.value = '2023-12-31T18:30:00Z'
compact.value = '2023-12-31T18:30:00Z'
}
const iso = (value: DatePickerValue) => {
if (value === null || Array.isArray(value)) return 'No selection'
const date = value instanceof Date ? value : new Date(value)
return Number.isNaN(date.getTime()) ? 'Invalid date' : date.toISOString()
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="resetUtc">Load the same instant as UTC</HuiButton>
<HuiSpace direction="vertical" fill>
<span>Z: ±HH:mm</span>
<HuiDatePicker
v-model="colon"
type="datetime"
format="YYYY-MM-DDTHH:mm:ssZ"
value-format="YYYY-MM-DDTHH:mm:ssZ"
style="width: 100%; max-width: 360px"
clearable
/>
<code>Bound value: {{ colon }}</code>
<code>UTC instant: {{ iso(colon) }}</code>
</HuiSpace>
<HuiSpace direction="vertical" fill>
<span>ZZ: ±HHmm</span>
<HuiDatePicker
v-model="compact"
type="datetime"
format="YYYY-MM-DDTHH:mm:ssZZ"
value-format="YYYY-MM-DDTHH:mm:ssZZ"
style="width: 100%; max-width: 360px"
clearable
/>
<code>Bound value: {{ compact }}</code>
<code>UTC instant: {{ iso(compact) }}</code>
</HuiSpace>
</HuiSpace>
</template>
Numeric timestamps
value-format="x" binds millisecond numbers, while value-format="X" binds second numbers. They affect the bound value without changing the displayed date or typed-input format. Range endpoints are also numbers; clearing still produces null.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerValue } from '@nscode/handy-ui'
const milliseconds = ref<DatePickerValue>(new Date(2024, 0, 15).getTime())
const seconds = ref<DatePickerValue>(Math.floor(new Date(2024, 0, 15, 9, 30).getTime() / 1000))
const range = ref<DatePickerValue>([
Math.floor(new Date(2024, 0, 15).getTime() / 1000),
Math.floor(new Date(2024, 0, 20).getTime() / 1000)
])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span>x: milliseconds</span>
<HuiDatePicker v-model="milliseconds" value-format="x" clearable />
<code>{{ typeof milliseconds }}:{{ milliseconds }}</code>
</HuiSpace>
<HuiSpace wrap>
<span>X: seconds</span>
<HuiDatePicker v-model="seconds" type="datetime" value-format="X" clearable />
<code>{{ typeof seconds }}:{{ seconds }}</code>
</HuiSpace>
<HuiSpace wrap>
<span>X: second range</span>
<HuiDatePicker v-model="range" type="daterange" value-format="X" clearable />
<code>{{ JSON.stringify(range) }}</code>
</HuiSpace>
</HuiSpace>
</template>
Disabled dates and panel options
pickerOptions accepts:
disabledDate(date): returntrueto prevent selection. Typing a disabled date also restores the previous value. A month or year cell is disabled only when all dates within it are disabled.cellClassName(date): return a class to add to a date cell.firstDayOfWeek: 1–7; 7 means Sunday and is the default.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const pickerOptions: DatePickerOptions = {
disabledDate: (date) => date.getTime() < new Date().setHours(0, 0, 0, 0),
firstDayOfWeek: 1
}
</script>
<template>
<HuiDatePicker v-model="value" :picker-options="pickerOptions" style="width: 240px" />
</template>
Disabled dates
disabledDate receives the cell date at local midnight. It works with every type: individual dates are disabled in date mode; neither endpoint of a range may be disabled; month and year cells become disabled only when every date within them is disabled. In the month example below, the current month still contains selectable days, while subsequent months do not.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const today = new Date()
today.setHours(0, 0, 0, 0)
const DAY = 24 * 3600 * 1000
const beforeToday: DatePickerOptions = {
disabledDate: (date) => date.getTime() < today.getTime()
}
const weekend: DatePickerOptions = {
disabledDate: (date) => date.getDay() === 0 || date.getDay() === 6
}
const last30Days: DatePickerOptions = {
disabledDate: (date) =>
date.getTime() > today.getTime() || date.getTime() < today.getTime() - 29 * DAY
}
const afterToday: DatePickerOptions = {
disabledDate: (date) => date.getTime() > today.getTime()
}
const before2020: DatePickerOptions = {
disabledDate: (date) => date.getFullYear() < 2020
}
const value1 = ref<Date | null>(null)
const value2 = ref<Date | null>(null)
const value3 = ref<[Date, Date] | null>(null)
const value4 = ref<Date | null>(null)
const value5 = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">No past dates</span>
<HuiDatePicker v-model="value1" :picker-options="beforeToday" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">No weekends</span>
<HuiDatePicker v-model="value2" :picker-options="weekend" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Last 30 days only</span>
<HuiDatePicker
v-model="value3"
type="daterange"
:picker-options="last30Days"
style="width: 300px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">month: through today</span>
<HuiDatePicker
v-model="value4"
type="month"
:picker-options="afterToday"
style="width: 240px"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">year: from 2020</span>
<HuiDatePicker
v-model="value5"
type="year"
:picker-options="before2020"
style="width: 240px"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 120px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Type a past date into the first input and press Enter. The input restores its previous value.
First day of the week
firstDayOfWeek sets the first weekday column: 1–6 mean Monday–Saturday and 7 means Sunday (default).
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Monday (1)</span>
<HuiDatePicker v-model="value" :picker-options="{ firstDayOfWeek: 1 }" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Saturday (6)</span>
<HuiDatePicker v-model="value" :picker-options="{ firstDayOfWeek: 6 }" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Sunday (7, default)</span>
<HuiDatePicker v-model="value" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 104px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Cell and panel classes
cellClassName adds classes to date cells; popperClass adds one to the whole dropdown panel. The panel is rendered under body, outside a component’s <style scoped>. Use an unscoped style and scope selectors with popperClass so other pickers are unaffected. This example marks weekends red and adds a dot below the first and fifteenth of each month:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const pickerOptions: DatePickerOptions = {
cellClassName: (date) => {
const classes: string[] = []
if (date.getDay() === 0 || date.getDay() === 6) classes.push('is-weekend')
if (date.getDate() === 1 || date.getDate() === 15) classes.push('is-marked')
return classes.join(' ')
}
}
</script>
<template>
<HuiDatePicker
v-model="value"
:picker-options="pickerOptions"
popper-class="demo-marked-panel"
style="width: 240px"
/>
</template>
<style>
/* Affect only this month; selected dates retain their white text. */
.demo-marked-panel
.hui-date-picker-panel__cell--normal.is-weekend:not(.hui-date-picker-panel__cell--selected) {
color: var(--hui-color-danger);
}
.demo-marked-panel .hui-date-picker-panel__cell.is-marked {
position: relative;
}
.demo-marked-panel .hui-date-picker-panel__cell.is-marked::after {
content: '';
position: absolute;
bottom: 3px;
left: 50%;
width: 4px;
height: 4px;
margin-left: -2px;
border-radius: 50%;
background-color: var(--hui-color-warning);
}
</style>
Shortcuts
pickerOptions.shortcuts renders shortcuts beside the panel. Two forms are supported:
{ text, value }: clicking selectsvalue. For a range, use[Date, Date].{ text, onClick(picker) }: clicking callsonClickwith the picker instance; callpicker.pick(...)there.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const pickerOptions: DatePickerOptions = {
// DatePickerShortcut[] | DatePickerOption[]; do not mix the two forms in one array.
shortcuts: [
{ text: 'Today', onClick: (picker) => picker.pick(new Date()) },
{
text: 'One week ago',
onClick: (picker) => picker.pick(new Date(Date.now() - 7 * 24 * 3600 * 1000))
}
]
}
</script>
<template>
<HuiDatePicker v-model="value" :picker-options="pickerOptions" style="width: 240px" />
</template>
The { text, value } examples below show date and range shortcuts. These values are calculated when defined, so they suit fixed dates. Use onClick above when a shortcut must calculate its date at click time, for example after a page has remained open all day.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerOptions } from '@nscode/handy-ui'
const DAY = 24 * 3600 * 1000
const today = new Date()
today.setHours(0, 0, 0, 0)
const date = ref<Date | null>(null)
const range = ref<[Date, Date] | null>(null)
const dateOptions: DatePickerOptions = {
shortcuts: [
{ text: 'Yesterday', value: new Date(today.getTime() - DAY) },
{
text: 'First day of this month',
value: new Date(today.getFullYear(), today.getMonth(), 1)
},
{ text: 'New Year’s Day', value: new Date(today.getFullYear(), 0, 1) }
]
}
const rangeOptions: DatePickerOptions = {
shortcuts: [
{ text: 'Last week', value: [new Date(today.getTime() - 6 * DAY), today] },
{ text: 'Last month', value: [new Date(today.getTime() - 29 * DAY), today] },
{ text: 'Last three months', value: [new Date(today.getTime() - 89 * DAY), today] }
]
}
</script>
<template>
<HuiSpace wrap>
<HuiDatePicker v-model="date" :picker-options="dateOptions" style="width: 240px" />
<HuiDatePicker
v-model="range"
type="daterange"
:picker-options="rangeOptions"
style="width: 300px"
/>
</HuiSpace>
</template>
Default month
Without a value, the panel shows the current month. defaultValue supplies another initial month. Range types accept two dates for the left and right panels. Enable unlinkPanels for nonadjacent months; otherwise the right panel stays one month after the left.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const range = ref<[Date, Date] | null>(null)
const rangeDefault: [Date, Date] = [new Date(2020, 0, 1), new Date(2020, 5, 1)]
</script>
<template>
<HuiSpace wrap>
<HuiDatePicker v-model="value" :default-value="new Date(2020, 5, 1)" style="width: 240px" />
<HuiDatePicker
v-model="range"
type="daterange"
:default-value="rangeDefault"
unlink-panels
style="width: 300px"
/>
</HuiSpace>
</template>
Clearable
clearable shows a clear button when a value exists, except while disabled or readonly. Clicking emits update:modelValue(null), clear and change(null) in that order.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date())
const range = ref<[Date, Date] | null>([new Date(), new Date(Date.now() + 7 * 24 * 3600 * 1000)])
const clearCount = ref(0)
</script>
<template>
<HuiSpace wrap>
<HuiDatePicker v-model="value" clearable style="width: 240px" @clear="clearCount++" />
<HuiDatePicker
v-model="range"
type="daterange"
clearable
style="width: 300px"
@clear="clearCount++"
/>
</HuiSpace>
<p style="margin-top: 12px">Cleared {{ clearCount }} times</p>
</template>
Typed input
By default, typed text applies on Enter or blur. Formats without offset tokens first use format, then common forms such as 2024-1-5 and 2024/1/5. Formats containing Z/ZZ are strict and do not fall back after failure. Unrecognized or disabled dates restore the previous value. A successful edit moves the panel to that month.
With editable=false, typing is disabled. Click the input area to open the panel, or press Space while focused to toggle it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Editable (default)</span>
<HuiDatePicker v-model="value" placeholder="Try 2024/1/5" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">editable=false</span>
<HuiDatePicker v-model="value" :editable="false" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 120px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Sizes
size accepts large, default and small. Input heights match Input and Button at 40 / 32 / 24px; font sizes are 16 / 14 / 12px, radii 8 / 6 / 4px, and calendar, clear and arrow icons 18 / 16 / 14px. This applies to every picker type, including both range dates and the separator. Panel dimensions do not change. When unset, size follows the containing HuiForm / HuiFormItem, then HuiConfigProvider, and finally defaults to default.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date())
const range = ref<[Date, Date] | null>([new Date(), new Date(Date.now() + 7 * 24 * 3600 * 1000)])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiDatePicker v-model="value" size="large" clearable style="width: 220px" />
<HuiDatePicker v-model="range" type="daterange" size="large" style="width: 320px" />
<HuiButton size="large">Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiDatePicker v-model="value" clearable style="width: 220px" />
<HuiDatePicker v-model="range" type="daterange" style="width: 320px" />
<HuiButton>Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiDatePicker v-model="value" size="small" clearable style="width: 220px" />
<HuiDatePicker v-model="range" type="daterange" size="small" style="width: 320px" />
<HuiButton size="small">Button</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Disabled and readonly
Both disabled and readonly prevent opening, typing and keyboard actions. Disabled controls use disabled text and background colors. Readonly controls retain their colors, use a normal pointer and do not change border color on hover.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(new Date())
const range = ref<[Date, Date] | null>([new Date(), new Date(Date.now() + 7 * 24 * 3600 * 1000)])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">disabled</span>
<HuiDatePicker v-model="value" disabled style="width: 240px" />
<HuiDatePicker v-model="range" type="daterange" disabled style="width: 300px" />
<HuiDatePicker type="date" disabled placeholder="No value" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">readonly</span>
<HuiDatePicker v-model="value" readonly style="width: 240px" />
<HuiDatePicker v-model="range" type="daterange" readonly style="width: 300px" />
<HuiDatePicker type="date" readonly placeholder="No value" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Custom icons
prefixIcon defaults to calendar; an empty string hides it. suffixIcon is hidden by default and appears before the dropdown arrow when supplied.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Default</span>
<HuiDatePicker v-model="value" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">No prefix</span>
<HuiDatePicker v-model="value" prefix-icon="" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Custom prefix</span>
<HuiDatePicker v-model="value" type="datetime" prefix-icon="clock" style="width: 240px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Suffix only</span>
<HuiDatePicker v-model="value" prefix-icon="" suffix-icon="calendar" style="width: 240px" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Event example
Interact with this picker to see recent events, newest first. Date or time edits emit change; opening or closing emits visibleChange; navigation emits panelChange; clearing emits clear. Use @visible-change and @panel-change in templates.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDatePicker } from '@nscode/handy-ui'
import type { DatePickerValue } from '@nscode/handy-ui'
const value = ref<Date | null>(null)
const logs = ref<string[]>([])
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onChange = (val: DatePickerValue) => {
log(`change:${val instanceof Date ? val.toLocaleString() : String(val)}`)
}
const onVisibleChange = (visible: boolean) => log(`visibleChange:${visible}`)
const onPanelChange = (date: Date, mode: string, view: string) => {
log(`panelChange: ${date.getFullYear()}-${date.getMonth() + 1}, mode=${mode}, view=${view}`)
}
</script>
<template>
<HuiDatePicker
v-model="value"
type="datetime"
clearable
style="width: 240px"
@change="onChange"
@focus="log('focus')"
@blur="log('blur')"
@clear="log('clear')"
@visible-change="onVisibleChange"
@panel-change="onPanelChange"
/>
<div class="event-log">
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
<div v-if="!logs.length">No events yet</div>
</div>
</template>
<style scoped>
.event-log {
margin-top: 12px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
line-height: 1.8;
}
</style>
Method example
Use a ref typed as DatePickerExpose to call focus(), blur(), open(), close(), clear() and pick(). Like clicking a date, pick() emits update:modelValue and change; types without time inputs close after selection. clear() shares the clear icon's handler and requests null for both single and range values, in update:modelValue → clear → change order. clearable only controls the icon, so it does not disable the method. Clearing is ignored while disabled or readonly, or after unmount.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerExpose } from '@nscode/handy-ui'
const pickerRef = ref<DatePickerExpose>()
const value = ref<Date | null>(null)
// Open the panel and close it after three seconds.
const openForAWhile = () => {
pickerRef.value?.open()
setTimeout(() => pickerRef.value?.close(), 3000)
}
const pickNewYear = () => {
pickerRef.value?.pick(new Date(new Date().getFullYear() + 1, 0, 1))
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiDatePicker ref="pickerRef" v-model="value" clearable style="width: 240px" />
<HuiSpace wrap>
<HuiButton @click="pickerRef?.focus()">Focus (focus)</HuiButton>
<HuiButton @click="openForAWhile">Open for 3 seconds (open / close)</HuiButton>
<HuiButton @click="pickerRef?.close()">Close (close)</HuiButton>
<HuiButton @click="pickerRef?.clear()">Clear (clear, requests null)</HuiButton>
<HuiButton @click="pickNewYear">Select next New Year (pick)</HuiButton>
</HuiSpace>
<p>JSON: {{ JSON.stringify({ date: value }) }}</p>
</HuiSpace>
</template>
Keyboard interaction
While the input is focused:
| Key | Action |
|---|---|
Enter | Toggle the panel; if text was edited, apply it and close |
↓ | Open the panel; if already open, focus the selected date, or today when empty |
Esc | Close the panel |
Tab | Close and move focus to the next page control as usual |
Space | Toggle the panel when editable=false |
While the panel is focused:
| Key | Action |
|---|---|
← → | Move one date, month or year cell |
↑ ↓ | Move one week among dates, or one row (4 cells) among months or years |
Enter, Space | Select the focused cell or activate the focused navigation, title, Today or shortcut button |
Esc | Close and return focus to the input |
Tab | Move among navigation, titles, the grid, time inputs, Today and shortcuts; leaving the final control (or the first with Shift+Tab) closes the panel and returns focus to the input |
- Arrow keys move focus without selecting; press Enter to select. Date navigation can move to another month. Month and year cells move within the current page, skipping disabled cells; use the header to change pages.
- Each grid has one Tab stop: the last keyboard position, selected cell, or today (current month or year).
- Disabled dates can receive arrow-key focus, but Enter does not select them. Disabled month or year cells, whose entire period is disabled, are skipped.
- When selection closes the panel, focus returns to the input. Time types and the first endpoint of a range leave the panel open and keep focus on the selected cell.
- After selecting the start of a range, arrow-key navigation also previews the range.
- The input has
role="combobox",aria-haspopup="dialog"andaria-expanded. The panel hasrole="dialog"and takes its name from the placeholder; once it has been opened, the input points to it witharia-controls. Wheneditableisfalse, these attributes are on the outerrole="button"element instead.
Saved and controlled values
Omitting modelValue, including listener-only usage, starts empty and saves accepted local selections. Passing the prop makes it controlled, even when explicitly undefined. Display, panel selection and form change validation use the actual parent value. update:modelValue and change report a requested value; rejection or normalization does not commit the candidate to the UI. Range first endpoints and input/time edits remain drafts. Clear requests null; rejected clear keeps the actual selection. Removing the prop restores the earlier internal value.
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiDatePicker, HuiSpace } from '@nscode/handy-ui'
import type { DatePickerExpose, DatePickerType, DatePickerValue } from '@nscode/handy-ui'
const types: DatePickerType[] = [
'year',
'month',
'date',
'datetime',
'daterange',
'datetimerange'
]
const type = ref<DatePickerType>('date')
const redraw = ref(0)
const local = ref<DatePickerExpose>()
const last = ref('No request')
const value = ref<DatePickerValue>(null)
const fixed = computed<DatePickerValue>(() =>
type.value.endsWith('range')
? [new Date(2026, 10, 20), new Date(2026, 10, 21)]
: new Date(2026, 10, 20)
)
const showRequest = (next: DatePickerValue) => {
last.value = JSON.stringify(next)
}
</script>
<template>
<HuiSpace class="date-picker-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton
v-for="item in types"
:key="item"
size="small"
:type="type === item ? 'primary' : 'default'"
@click="type = item"
>{{ item }}</HuiButton
>
<HuiButton class="date-redraw" @click="redraw++">Parent redraw {{ redraw }}</HuiButton>
<HuiButton class="date-clear-local" @click="local?.clear()">Clear first</HuiButton>
</HuiSpace>
<HuiSpace wrap :key="type">
<div class="date-local">
<p>Omitted: saved internally</p>
<HuiDatePicker ref="local" :type="type" clearable />
</div>
<div class="date-listener">
<p>Listener only</p>
<HuiDatePicker :type="type" clearable @update:model-value="showRequest" />
</div>
<div class="date-empty">
<p>Explicit undefined: stays empty</p>
<HuiDatePicker
:type="type"
:model-value="undefined"
clearable
@update:model-value="showRequest"
/>
</div>
<div class="date-fixed">
<p>Fixed parent value: rejects updates</p>
<HuiDatePicker
:type="type"
:model-value="fixed"
clearable
@update:model-value="showRequest"
/>
</div>
<div class="date-bound">
<p>Two-way binding</p>
<HuiDatePicker v-model="value" :type="type" clearable />
</div>
</HuiSpace>
<p class="date-status">Bound value: {{ JSON.stringify(value) }}; last request: {{ last }}</p>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | DatePickerValue | undefined | Omitted or listener-only saves locally from null; passing it is controlled, including explicit undefined. |
| type | 'year' | 'month' | 'date' | 'datetime' | 'daterange' | 'datetimerange' | 'date' | Picker type |
| size | 'large' | 'default' | 'small' | — | Inherit from Form, then ConfigProvider; otherwise default |
| placeholder | string | By type and locale | Placeholder for year, month, date or datetime selection |
| startPlaceholder | string | By type and locale | Range start placeholder; start date or start time for datetimerange |
| endPlaceholder | string | By type and locale | Range end placeholder; end date or end time for datetimerange |
| disabled | boolean | false | Disable interaction |
| readonly | boolean | false | Prevent editing while retaining normal colors |
| editable | boolean | true | Allow typed input |
| clearable | boolean | false | Show the clear button |
| format | string | By type | Display format, also used to parse typed text |
| valueFormat | string | — | Unset emits Date; x emits millisecond numbers, X emits second numbers, and other formats emit strings |
| pickerOptions | DatePickerOptions | — | Disabled dates, shortcuts, cell classes and first weekday |
| popperClass | string | — | Class added to the dropdown panel |
| rangeSeparator | string | By locale | Separator between range values |
| defaultValue | Date | [Date, Date] | — | Initial panel month when no value is selected |
| defaultTime | Date | [Date, Date] | — | Time fields used for date selection; two values for a range |
| unlinkPanels | boolean | false | Navigate range panels independently |
| prefixIcon | string | 'calendar' | Prefix icon; an empty string hides it |
| suffixIcon | string | — | Suffix icon |
| validateEvent | boolean | true | Validate changes or focus leaving the whole control. Internal focus moves or closing back to the trigger do not run blur validation |
DatePickerOptions
| Property | Type | Description |
|---|---|---|
| disabledDate | (date: Date) => boolean | Return true to prevent selection; also applies to typed input |
| shortcuts | DatePickerShortcut[] | DatePickerOption[] | Shortcuts in either form described above |
| cellClassName | (date: Date) => string | Class added to each date cell |
| firstDayOfWeek | number | First weekday, 1–7; 7 means Sunday and is the default |
Events
| Event | Parameters | Description |
|---|---|---|
| update:modelValue | (value: DatePickerValue) | Date or time selection, typed-input commit, shortcuts or clearing; used by v-model |
| change | (value: DatePickerValue) | Reports the requested date/time or null; display and form validation use the actual accepted value. |
| focus | (event: FocusEvent) | Focus enters the trigger or panel from outside the control; internal moves do not emit again |
| blur | (event: FocusEvent) | Focus leaves both trigger and panel. Internal moves or closing back to the trigger do not emit blur |
| clear | — | Clear button is clicked |
| visibleChange | (visible: boolean) | Panel visibility actually changes |
| panelChange | (value: Date, mode: 'year' | 'month', view: 'year' | 'month' | 'date') | Navigation or year/month selection; value is the first day of the displayed month, mode identifies the changed unit and view identifies the current view |
Slots
No public slots.
Methods
| Method | Signature | Description |
|---|---|---|
| focus | () => void | Focus the actual trigger without opening the panel; calling from the panel returns to the trigger |
| blur | () => void | Remove focus currently inside the trigger or panel from the whole control |
| open | () => void | Open the panel unless disabled or readonly |
| close | () => void | Close the panel |
| clear | () => void | Request null; ignored while disabled or readonly, or after unmount. Independent of the clearable icon setting |
| pick | (value: Date | [Date, Date]) => void | Select directly. Range types require a pair; mismatched types are ignored. Types without time inputs close after selection |
Types
Public types use the component prefix. Compatibility mappings: DateCell → DatePickerCell, PickerOption → DatePickerOption, PickerOptions → DatePickerOptions, and PickerShortcut → DatePickerShortcut. The old names remain importable from @nscode/handy-ui, are marked @deprecated, and will be removed in 1.0.
import type {
DatePickerType,
DatePickerValue,
DatePickerOptions,
DatePickerShortcut,
DatePickerOption,
DatePickerExpose
} from '@nscode/handy-ui'
type DatePickerType = 'year' | 'month' | 'date' | 'datetime' | 'daterange' | 'datetimerange'
type DatePickerValue =
| Date
| string
| number
| [Date | string | number, Date | string | number]
| null
interface DatePickerOptions {
disabledDate?: (date: Date) => boolean
shortcuts?: DatePickerShortcut[] | DatePickerOption[]
cellClassName?: (date: Date) => string
firstDayOfWeek?: number
}
interface DatePickerShortcut {
text: string
value: Date | [Date, Date]
}
interface DatePickerOption {
text: string
onClick: (picker: DatePickerExpose) => void
}
interface DatePickerExpose {
focus: () => void
blur: () => void
open: () => void
close: () => void
clear: () => void
pick: (date: Date | [Date, Date]) => void
}
/** @deprecated Use DatePickerExpose. Removed in 1.0. */
type DatePickerInstance = DatePickerExpose