Datepicker

The <nys-datepicker> component allows users to select a date from a visual calendar or type a date into the input field.

Copy Code
<nys-datepicker
  id="my-datepicker"
  name="my-datepicker"
  label="Schedule your appointment"
  description="Enter in MM/DD/YYYY format"
></nys-datepicker>

Options

The calendar interface does not appear in Safari or mobile browsers (iOS and Android). In those environments, users enter dates manually or use the native calendar popup.

Value and form output

The value prop can be set as a Date object or an ISO date string (YYYY-MM-DD). Outwards, the input displays it as (mm/dd/yyyy). Internally, the component stores and submits the value as a string in ISO format.

Passing new Date("YYYY-MM-DD") to value can result in an off by one day error due to UTC versus local time conversion. Make sure the Date represents the intended local date.
Why this happens

<nys-datepicker> safely accepts ISO date strings ("YYYY-MM-DD") and handles display and submission correctly.

Valid value examples

Copy Code
// Using a Date object
value = new Date(2025, 5, 1) // June 1, 2025
// Using a string
value = "2025-06-01"
Copy Code
<nys-datepicker value="2025-06-01"></nys-datepicker>
Copy Code
// FormData submits a string in YYYY-MM-DD format
2025-06-01

Width

Set the width prop to adjust the width of <nys-datepicker> Supported widths are:

  • md: 200px (default width)
  • lg: 384px
  • full: will take up the full width of the parent container.
Copy Code
<div style="display: flex; flex-direction: column; gap: 16px;">
  <nys-datepicker label="Medium width" width="md"></nys-datepicker>
  <nys-datepicker label="Large width" width="lg"></nys-datepicker>
  <nys-datepicker label="Full width" width="full"></nys-datepicker>
</div>

Custom Start Date

Use this option to guide users toward a relevant time period without preselecting a date.

Copy Code
<nys-datepicker label="Project start date" startDate="2024-01-01"></nys-datepicker>

Hiding Buttons

Hide the Today and Clear buttons to require users to intentionally choose a date.
Use properties hideTodayButton and hideClearButton to toggle off the buttons

Copy Code
<nys-datepicker
  label="Select a date"
  hideTodayButton
  hideClearButton
></nys-datepicker>

Disabled

Copy Code
<nys-datepicker
  label="Disabled datepicker"
  disabled
  value="2025-01-15"
></nys-datepicker>

Usage

When to use this component

  • When users need to select a single calendar date
  • When a form requires consistent date formatting across browsers
  • When native date picker behavior needs to be replaced with a controlled, accessible experience

When to consider something else

  • Familiar dates where users know the value without a calendar, such as a date of birth. Consider memorable date fields (currently under proposal)
  • When the day of the week is irrelevant, such as birthdays or document issue dates. Consider memorable date fields (currently under proposal)
  • When users need to select multiple dates or a span of time. Consider a date range component (possible version 2 of nys-datepicker)
  • When time selection is required

Do

  • Use for deadlines, scheduling, and future dates
  • Pair with labels and helper text for clarity
  • Use required when the date must be provided

Don't

  • Don't use for memorable dates, or dates in the past, use a textinput instead
  • Don't use without a label or accessible description
  • Don't use if relying on browser specific date picker behavior

Accessibility

The <nys-datepicker> component includes the following accessibility-focused features:

  • Uses native <input type="date"> semantics for screen reader support
  • Supports keyboard interaction to open and navigate the calendar
  • Applies aria-required, aria-disabled, and validation messaging
  • Error messages are announced via associated error messaging

Properties

Property Type
id String
label String
description String
name String
value string
width "md" | "lg" | "full"
disabled boolean
required String
optional String
showError boolean
errorMessage String
startDate String (format MM/DD/YYYY)
hideTodayButton boolean
hideClearButton boolean
form String | null

CSS Variables

There are no existing CSS variables for this component. Explore existing options, or propose a new one with a Component Proposal.


Events

The <nys-datepicker> component emits two custom Javascript events:

  1. nys-input – Emitted when the datepicker input text changes.
  2. nys-blur – Emitted when the datepicker input loses focus.

Event details

The nys-input event includes a detail object with the following properties:

  • id (string): The id of the datepicker.
  • value (Date): The current value of the datepicker. Updates when a new valid date is set.

You can listen to these events using JavaScript:

Copy Code
// Select the datepicker component
const datepicker = document.querySelector("nys-datepicker");
// Listen for the 'nys-input' event
datepicker.addEventListener('nys-input', (event) => {
  const { id, value } = event.detail;
  console.log(`Datepicker (${id}) value has changed: `, value);
});

Suggest a New Component

Do you have an idea for a new NYS Design System web component? Look through the existing proposals in our GitHub discussions board to see if someone already proposed something similar. If not, feel free to submit one.