> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/pocketbase/pocketbase/llms.txt
> Use this file to discover all available pages before exploring further.

# Date field

> Store date and time values with optional min/max constraints

The date field stores a single date and time value using PocketBase's `types.DateTime` type. You can optionally enforce min/max date constraints for validation.

## Configuration options

<ParamField path="min" type="types.DateTime">
  Minimum allowed date value. Leave empty to skip the validator.
</ParamField>

<ParamField path="max" type="types.DateTime">
  Maximum allowed date value. Leave empty to skip the validator.
</ParamField>

<ParamField path="required" type="bool" default="false">
  When true, requires the field value to be a non-zero date time.
</ParamField>

## Validation rules

The date field validates:

* **Type**: Value must be a valid `types.DateTime`
* **Min date**: If specified, value must be on or after the minimum date
* **Max date**: If specified, value must be on or before the maximum date
* **Required**: If enabled, value cannot be zero (empty)

## Go examples

<Tabs>
  <Tab title="Basic usage">
    ```go theme={null}
    import (
        "github.com/pocketbase/pocketbase/core"
        "github.com/pocketbase/pocketbase/tools/types"
    )

    field := &core.DateField{
        Name:     "publishedAt",
        Required: false,
    }

    collection.Fields.Add(field)

    // Set field value
    now := types.NowDateTime()
    record.Set("publishedAt", now)
    ```
  </Tab>

  <Tab title="With min/max constraints">
    ```go theme={null}
    minDate, _ := types.ParseDateTime("2024-01-01 00:00:00.000Z")
    maxDate, _ := types.ParseDateTime("2024-12-31 23:59:59.999Z")

    field := &core.DateField{
        Name:     "eventDate",
        Required: true,
        Min:      minDate,
        Max:      maxDate,
    }

    collection.Fields.Add(field)

    // Set a date within range
    eventDate, _ := types.ParseDateTime("2024-06-15 14:30:00.000Z")
    record.Set("eventDate", eventDate)
    ```
  </Tab>

  <Tab title="Future dates only">
    ```go theme={null}
    field := &core.DateField{
        Name:     "expiresAt",
        Required: true,
        Min:      types.NowDateTime(), // Must be in the future
    }

    collection.Fields.Add(field)

    // Set expiration to 30 days from now
    expiry := types.NowDateTime()
    record.Set("expiresAt", expiry)
    ```
  </Tab>

  <Tab title="Birth date validation">
    ```go theme={null}
    maxDate := types.NowDateTime() // Cannot be in the future
    minDate, _ := types.ParseDateTime("1900-01-01 00:00:00.000Z")

    field := &core.DateField{
        Name:     "birthDate",
        Required: true,
        Min:      minDate,
        Max:      maxDate,
    }

    collection.Fields.Add(field)

    birthDate, _ := types.ParseDateTime("1990-05-15 00:00:00.000Z")
    record.Set("birthDate", birthDate)
    ```
  </Tab>
</Tabs>

## Database column type

```sql theme={null}
TEXT DEFAULT '' NOT NULL
```

<Info>
  Dates are stored as ISO 8601 formatted strings in the database but handled as `types.DateTime` objects in Go.
</Info>

## Date format

PocketBase uses ISO 8601 format for dates:

<CodeGroup>
  ```go Parsing dates theme={null}
  import "github.com/pocketbase/pocketbase/tools/types"

  // From string
  date, err := types.ParseDateTime("2024-03-15 14:30:00.000Z")

  // Current time
  now := types.NowDateTime()

  // From time.Time
  import "time"
  t := time.Now()
  date := types.DateTime{Time: t}
  ```

  ```go Getting values theme={null}
  // Get as DateTime
  date := record.GetDateTime("publishedAt")

  // Check if zero
  if date.IsZero() {
      // Field is empty
  }

  // Get as time.Time
  t := date.Time()

  // Get as string
  str := date.String()
  ```
</CodeGroup>

## Common use cases

<CodeGroup>
  ```go Timestamp fields theme={null}
  field := &core.DateField{
      Name:     "deadline",
      Required: true,
  }
  ```

  ```go Event scheduling theme={null}
  minDate := types.NowDateTime()

  field := &core.DateField{
      Name:     "eventDate",
      Required: true,
      Min:      minDate, // Events must be in the future
  }
  ```

  ```go Expiration dates theme={null}
  field := &core.DateField{
      Name: "expiresAt",
      Min:  types.NowDateTime(),
  }
  ```
</CodeGroup>

## Best practices

<Note>
  * Use `types.NowDateTime()` for current timestamp
  * Set appropriate min/max constraints to prevent invalid dates
  * For created/updated timestamps, consider using the autodate field instead
  * Dates are stored in UTC, plan timezone handling accordingly
  * Use `IsZero()` to check if a date field is empty
</Note>

## Zero value

The zero value for date fields is a zero `types.DateTime`, which you can check using the `IsZero()` method.
