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.
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 valuenow := types.NowDateTime()record.Set("publishedAt", now)
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 rangeeventDate, _ := types.ParseDateTime("2024-06-15 14:30:00.000Z")record.Set("eventDate", eventDate)
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 nowexpiry := types.NowDateTime()record.Set("expiresAt", expiry)
maxDate := types.NowDateTime() // Cannot be in the futureminDate, _ := 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)