Skip to main content
The autodate field automatically sets the current date and time when records are created or updated. It’s commonly used for created and updated timestamp fields.

Configuration options

onCreate
bool
default:"false"
When true, automatically sets the current datetime when the record is created.
onUpdate
bool
default:"false"
When true, automatically updates to the current datetime whenever the record is updated.
At least one of onCreate or onUpdate must be enabled. Both can be enabled simultaneously.

How it works

The autodate field has special behavior:
  1. Automatic setting: The value is automatically set during create/update operations
  2. Read-only via Set(): Calling record.Set() on an autodate field has no effect
  3. Manual override: You can manually set values using record.SetRaw() if needed
  4. System fields: If the field is marked as system, the onCreate and onUpdate settings cannot be changed
The autodate field uses a no-op setter, which means record.Set() calls are ignored. Use record.SetRaw() if you need to manually override the timestamp.

Go examples

import "github.com/pocketbase/pocketbase/core"

field := &core.AutodateField{
    Name:     "created",
    OnCreate: true,
    OnUpdate: false,
}

collection.Fields.Add(field)

// Value is automatically set on create
record := core.NewRecord(collection)
app.Save(record)
// created field now contains the current timestamp

Database column type

TEXT DEFAULT '' NOT NULL
Autodate fields store timestamps as ISO 8601 formatted strings, the same as regular date fields.

Common patterns

// Created timestamp (set once)
collection.Fields.Add(&core.AutodateField{
    Name:     "created",
    OnCreate: true,
    OnUpdate: false,
})

// Updated timestamp (always current)
collection.Fields.Add(&core.AutodateField{
    Name:     "updated",
    OnCreate: true,
    OnUpdate: true,
})

System fields

Base collections automatically include autodate system fields:
// These are automatically added to every collection
&AutodateField{
    Name:     "created",
    System:   true,
    OnCreate: true,
    OnUpdate: false,
}

&AutodateField{
    Name:     "updated",
    System:   true,
    OnCreate: true,
    OnUpdate: true,
}
System autodate fields cannot have their onCreate or onUpdate settings changed to maintain consistency across all PocketBase collections.

Reading autodate values

import "github.com/pocketbase/pocketbase/tools/types"

created := record.GetDateTime("created")
updated := record.GetDateTime("updated")

// Check if set
if !created.IsZero() {
    // Has a value
}

Filtering by autodate fields

import "time"
import "github.com/pocketbase/pocketbase/tools/types"

// Records created in the last 24 hours
last24h := types.DateTime{Time: time.Now().Add(-24 * time.Hour)}
records, err := app.FindRecordsByFilter(
    "articles",
    "created > {:date}",
    "-created",
    10,
    0,
    dbx.Params{"date": last24h},
)

Use cases

When to use onCreate only:
  • Tracking creation timestamp (standard created field)
  • Recording when content was first published
  • Audit trail: when a record was first inserted
When to use onUpdate only:
  • Tracking last modification (when you don’t care about creation)
  • Cache invalidation timestamps
When to use both:
  • Standard updated field that needs an initial value
  • Activity tracking that should always show last interaction
  • Last accessed/viewed timestamps

Best practices

  • Use the standard created and updated system fields when possible
  • Only create additional autodate fields when you need specific behavior
  • Remember that record.Set() doesn’t work on autodate fields
  • Use SetRaw() for manual overrides (like backdating records)
  • Consider using regular date fields if you need user-controllable dates
  • Autodate fields are always in UTC

Comparison with date field

FeatureAutodateDate
Automatic settingYesNo
Can use record.Set()No (use SetRaw())Yes
ValidationNoneMin/Max/Required
Use caseTimestampsUser input dates

Zero value

The zero value for autodate fields is a zero types.DateTime, but in practice autodate fields are typically always set automatically.