Date objects, and how to convert values from your application’s date library.
String format
Date field components accept and return date-only ISO strings inYYYY-MM-DD format (for example, "2025-07-02").
Full ISO datetime strings (for example, "2025-07-02T00:00:00.000Z") are also accepted as input. Date fields normalize these values to the calendar date and emit date-only strings from onChange callbacks.
| Direction | Format | Example |
|---|---|---|
Input (value, defaultValue, minDate, maxDate) | ISO 8601 date string | "2025-07-02" |
Output (onChange) | ISO 8601 date string or null | "2025-07-02" or null |
| Range output | Object with startDate and endDate strings or null | { startDate: "2025-07-01", endDate: "2025-07-31" } |
Date Field Yearless and Date Field Yearless Range use
{ month, day } objects instead of ISO strings because they represent recurring dates without a year. See Date Field Yearless and Date Field Yearless Range for those APIs.Calendar accepts date-only strings as input but returns full ISO datetime strings from onSelection (for example, “2025-07-02T00:00:00.000-07:00”). Convert Calendar output with value?.split("T")[0] (handle undefined) or a date library before passing it to date fields.Why strings instead of JavaScript Date objects
Anvil2 date components use strings rather than JavaScriptDate objects for three reasons:
- Library neutrality — Teams across ServiceTitan use different date libraries (Luxon, Moment, Dispatch Temporal-lite, and others). ISO strings provide a single interchange format that any library can parse and produce without coupling components to one runtime type.
- Predictable calendar dates — JavaScript
Dateobjects carry timezone and time-of-day information. Passing aDatebetween layers can shift the displayed calendar day. Date-only strings represent the intended calendar date directly. - API alignment — Backend services and generated API clients often serialize dates as ISO strings. Strings map directly to form state, request payloads, and URL parameters without intermediate conversion at the component boundary.
Date objects. When migrating to Anvil2, convert Date values to ISO strings before passing them to date fields. See the Anvil to Anvil2 migration guide for conversion patterns.
Components that use date strings
The following Anvil2 components use ISO date strings:| Component | Value prop | Output |
|---|---|---|
| Date Field Single | string | null | change.date (date-only) |
| Date Field Range | { startDate: string | null; endDate: string | null } | null | change.startDate, change.endDate (date-only) |
| Calendar | string or { start, end } (date-only input) | onSelection (full ISO datetime) |
| Filter Bar date filters | ISO string, range object, or null | Filter value |
Convert from JavaScript Date
When your application stores dates as JavaScriptDate objects, convert to a date-only string before passing the value to date fields. If your API client already returns ISO date strings, pass them directly without conversion.
getFullYear, getMonth, getDate) rather than toISOString() when converting from a JavaScript Date. The toISOString() method returns UTC, which can produce a different calendar date in some timezones.
Convert from Luxon
Anvil2 uses Luxon internally for date parsing. If your application already uses Luxon, usetoISODate():