# Calendar A date picker that lets the user click to select a single date. The selected date is stored in a hidden form input and submitted with the form. Think of it as a fancy `` that looks the same on every browser. --- ## Quick example ```csharp new Calendar(id: "dob", name: "dateOfBirth") ``` That renders a calendar starting at today's date. When the user clicks a day, the hidden input is updated and the date is included in the form submission. --- ## All the options ```csharp public Calendar( string id, string name = "date", DateOnly? selected = null) ``` | Parameter | What it does | |---|---| | `id` | A unique identifier for this calendar. The root element gets `id="cal-{id}"`. | | `name` | The form field name for the hidden input. Use this name in your `Command` record on the server. | | `selected` | The date to pre-select on render. Defaults to today. | --- ## Real-world examples ### Appointment booking form ```html
$$Token$$
$$DatePicker$$
``` ```csharp // Templates/BookingForm.htmx.cs public sealed class BookingForm : BookingFormBase { private readonly IHtmxComponent _datePicker; private readonly byte[] _tokenData; public BookingForm(string antiforgeryToken) { _tokenData = $"""""".ToUtf8Bytes(); _datePicker = new Calendar(id: "booking", name: "bookingDate"); } protected override void RenderToken(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_tokenData); protected override void RenderDatePicker(HtmxRenderContext ctx) => _datePicker.Render(ctx.Next()); } ``` **Reading the submitted date on the server:** ```csharp public record Command( [property: FromForm] string BookingDate // arrives as "yyyy-MM-dd" ); var date = DateOnly.ParseExact(command.BookingDate, "yyyy-MM-dd"); ``` ### Pre-selected date (e.g. editing an existing booking) ```csharp new Calendar( id: "appointment", name: "appointmentDate", selected: existingBooking.Date) ``` ### Reacting to date selection in JavaScript When the user picks a date, the calendar fires a `calendarChange` custom event: ```js document.getElementById('cal-booking').addEventListener('calendarChange', e => { console.log(e.detail.date); // "2026-09-15" // update price estimates, availability, etc. }); ``` --- ## How it works The calendar is rendered as static HTML by the server, with the current month's grid pre-built as `