# date

Validate date strings

Canonical URL: https://docs.paradoc.dev/sdk/primitives/date/

Validate date strings in ISO 8601 format (YYYY-MM-DD). The Date primitive ensures dates are properly formatted and represent valid calendar dates.

## Examples

### Creating dates

```ts
// Direct validation
const date = p.date('2024-01-15')

// Parse unknown input
const date = p.date.parse(jsonData)

// Check validity without throwing
if (p.date.isValid('2024-02-29')) {
  console.log('Valid leap year date')
}
```

### Parsing external data

```ts
// Parse unknown input (throws on error)
const date = p.date.parse(userInput)

// Safe parsing (returns result object)
const result = p.date.safeParse(userInput)
if (result.success) {
  console.log(`Date: ${result.data}`)
} else {
  console.error(result.error.message)
}
```

### Using with forms

```ts
const form = p.form({
  name: 'booking',
  fields: {
    checkIn: { type: 'date', label: 'Check-in Date' },
    checkOut: { type: 'date', label: 'Check-out Date' }
  }
})

const filled = form.fill({
  fields: {
    checkIn: p.date('2024-06-01'),
    checkOut: p.date('2024-06-07')
  }
})
```

## API

### Direct Call

```ts
p.date(input: string): string
```

Pass a date string for validation. Returns the validated string or throws on error.

```ts
const date = p.date('2024-01-15')
```

### Static Methods

| Method | Returns | Description |
| --- | --- | --- |
| `parse(input: unknown)` | `string` | Parse unknown input (throws on error) |
| `safeParse(input: unknown)` | `Result<string>` | Parse unknown input (returns result object) |
| `isValid(input: string)` | `boolean` | Check if a string is a valid date |

## Format

The Date primitive expects and returns strings in ISO 8601 date format:

```
YYYY-MM-DD
```

| Component | Description     | Range     |
| --------- | --------------- | --------- |
| `YYYY`    | Four-digit year | 0000-9999 |
| `MM`      | Two-digit month | 01-12     |
| `DD`      | Two-digit day   | 01-31     |

## Validation

The Date primitive validates:

* String must match `YYYY-MM-DD` format exactly
* Year, month, and day must be valid numbers
* Day must be valid for the given month (accounts for leap years)

```ts
// Valid
p.date('2024-01-15')
p.date('2024-02-29')  // Leap year
p.date('2000-12-31')

// Invalid - throws Error
p.date('2024-1-15')

p.date('2024-13-01')

p.date('2024-02-30')

p.date('2023-02-29')  // Not a leap year

p.date('not-a-date')
```

## Related

* [Primitives Overview](/sdk/primitives) - All available primitives
* [Datetime](/sdk/primitives/datetime) - Date with time component
* [Time](/sdk/primitives/time) - Time only
* [Date Field](/sdk/artifacts/form/field#date--datetime--time) - Date field type
