> For the complete documentation index, see [llms.txt](https://docs.forms.md/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.forms.md/input-types/time-input.md).

# Time input

<figure><img src="/files/qYYGx4O3Sgvi1OHqXpJl" alt=""><figcaption><p>Time input</p></figcaption></figure>

Use the `timeInput()` function to create time inputs in your forms. It uses the HTML `<input type="time">` element which provides built-in time validation.

***

## Basic usage

```javascript
import { Composer } from "formsmd";

const composer = new Composer({
  id: "my-form"
});

composer.timeInput("appointmentTime", {
  question: "What time would you like to schedule your appointment?"
});
```

Generates the following Markdown-like syntax:

```
#! id = my-form

appointmentTime = TimeInput(
  | question = What time would you like to schedule your appointment?
)
```

### Required

Add the `required` parameter to make the field mandatory:

```javascript
composer.timeInput("appointmentTime", {
  question: "What time would you like to schedule your appointment?",
  required: true
});
```

Generates the following Markdown-like syntax:

```
appointmentTime* = TimeInput(
  | question = What time would you like to schedule your appointment?
)
```

***

## Function overview

The following is the overview of the function:

```typescript
timeInput(name: string, params: object)
```

### Arguments

| Name     | Type     | Description                                                                                                                                                     |
| -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`   | `string` | A unique name for your form field that you'll use to identify the user's response.<mark style="color:red;">\*</mark>                                            |
| `params` | `object` | An object containing all the configuration parameters for your time input field (see the [parameters section](#parameters) below for the full list of options). |

{% hint style="danger" %} <mark style="color:red;">\*</mark>Avoid values for the `name` argument which may be the names of HTML attributes, such as `"name"`, `"role"`, `"id"`, etc. This is because by default, the form's template string is first sanitized using [DOMPurify](https://github.com/cure53/DOMPurify), and these values may be removed to prevent DOM clobbering.
{% endhint %}

***

## Parameters

### Shared parameters

These parameters are common to all form fields:

| Name                  | Type                                            | Description                                                                                                                                                                                                                                                                             |
| --------------------- | ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `question` (required) | `string`                                        | The main question or label of the form field.                                                                                                                                                                                                                                           |
| `required`            | `true` (`boolean`)                              | When set, the field becomes required.                                                                                                                                                                                                                                                   |
| `description`         | `string`                                        | Any extra information that the user may need to fill out the form. Appears right below the question.                                                                                                                                                                                    |
| `fieldSize`           | `"sm"`                                          | When set to `"sm"`, the font sizes of the question, description, and answer are made smaller.                                                                                                                                                                                           |
| `labelStyle`          | `"classic"`                                     | When set to `"classic"`, the question and description of the form field are made smaller.                                                                                                                                                                                               |
| `subfield`            | `true` (`boolean`)                              | When set, the question and description of the form field are made smaller. Functionally the same as setting `labelStyle` to `"classic"`.                                                                                                                                                |
| `disabled`            | `true` (`boolean`)                              | When set, the input is disabled.                                                                                                                                                                                                                                                        |
| `autofocus`           | `true` (`boolean`)                              | When set, the input will be automatically focused when the parent slide becomes active, or immediately after page load.                                                                                                                                                                 |
| `id`                  | `string`                                        | The `id` attribute of the form field container.                                                                                                                                                                                                                                         |
| `classNames`          | `string[]`                                      | The CSS class names of the form field. [See the available CSS utility classes](/content/css-utility-classes.md).                                                                                                                                                                        |
| `attrs`               | `Array<{ name: string, value: string }>`        | Other HTML attributes of the form field. Each attribute has a `name` and `value` property.                                                                                                                                                                                              |
| `displayCondition`    | `{ dependencies: string[], condition: string }` | Controls when the field is shown. The `dependencies` lists the fields to watch, and `condition` is the expression that must be true to show the field. The `condition` must be a valid [Nunjucks](https://mozilla.github.io/nunjucks/) expression. [See example](#conditional-display). |

### Time input specific parameters

| Name          | Type     | Description                                      |
| ------------- | -------- | ------------------------------------------------ |
| `placeholder` | `string` | Sets the `placeholder` attribute of the input.   |
| `min`         | `string` | Sets the minimum allowed time value (`"HH:mm"`). |
| `max`         | `string` | Sets the maximum allowed time value (`"HH:mm"`). |
| `step`        | `string` | Sets the stepping interval.                      |
| `value`       | `string` | Pre-selected time value (`"HH:mm"`).             |

***

## Examples

### Time input with min and max values

```javascript
composer.timeInput("meetingTime", {
  question: "What time would you like to schedule the meeting?",
  description: "Business hours are from 9 AM to 5 PM",
  min: "09:00",
  max: "17:00",
  required: true
});
```

Generates the following Markdown-like syntax:

```
meetingTime* = TimeInput(
  | question = What time would you like to schedule the meeting?
  | description = Business hours are from 9 AM to 5 PM
  | min = 09:00
  | max = 17:00
)
```

### Time input with step interval

```javascript
composer.timeInput("appointmentTime", {
  question: "Select an appointment time",
  description: "Appointments are available in 30-minute slots",
  step: "1800",
  required: true
});
```

Generates the following Markdown-like syntax:

```
appointmentTime* = TimeInput(
  | question = Select an appointment time
  | description = Appointments are available in 30-minute slots
  | step = 1800
)
```

### Styled time input with custom attributes

Add CSS classes and other HTML attributes using the `classNames` and `attrs` parameters. Please note, these class names and attributes are added to the `<div>` or `<fieldset>` container that contains the actual input field(s).

```javascript
composer.timeInput("preferredTime", {
  question: "Preferred time",
  classNames: ["col-6", "xs:col-6"],
  attrs: [
    { name: "style", value: "font-size: 18px;" }
  ]
});
```

Generates the following Markdown-like syntax:

```
[.col-6 .xs:col-6 style="font-size: 18px;"]
preferredTime = TimeInput(
  | question = Preferred time
)
```

Please [see the available CSS utility classes](/content/css-utility-classes.md).

### Conditional display

Conditionally show or hide a time input field using the `displayCondition` parameter. It works as follows:

* `dependencies` lists the fields to watch.
* `condition` is the expression that must be true to show the field. This must be a valid [Nunjucks](https://mozilla.github.io/nunjucks/) expression.

For instance, in the example below, the preferred meeting time input will only show up if the user indicates they want to schedule a meeting.

```javascript
composer.choiceInput("wantMeeting", {
  question: "Would you like to schedule a meeting?",
  required: true,
  choices: ["Yes", "No"]
})

composer.timeInput("meetingTime", {
  question: "Preferred meeting time",
  displayCondition: {
    dependencies: ["wantMeeting"],
    condition: "wantMeeting == 'Yes'"
  }
});
```

Generates the following Markdown-like syntax:

```
wantMeeting* = ChoiceInput(
  | question = Would you like to schedule a meeting?
  | choices = Yes, No
)

::: [{$ wantMeeting $}]
{% if wantMeeting == "Yes" %}
meetingTime = TimeInput(
  | question = Preferred meeting time
)
{% endif %}
:::
```
