dateformat

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dateformat renders and parses dates using a small user-facing token pattern (DD/MM/YYYY and friends) instead of a Go layout string.

Go layouts are unusable as a user setting: the reference-time components are ordinary text, so a literal in the user's pattern can silently become a format verb. The tokens here are matched explicitly and anything that is not a token is rejected at validation time rather than reinterpreted.

Index

Constants

View Source
const Auto = ""

Auto is the zero value of a user's date_format: keep the built-in locale-ish rendering each surface already used before this setting existed.

View Source
const TimePattern = "HH:mm"

TimePattern is appended to a date pattern to make a datetime pattern. The setting itself is date-only; times stay 24h everywhere.

Variables

View Source
var (
	ErrEmpty       = errors.New("empty pattern")
	ErrTooLong     = fmt.Errorf("pattern longer than %d characters", maxPatternLen)
	ErrStrayLetter = errors.New("pattern contains letters that are not tokens")
	ErrNoYear      = errors.New("pattern must contain a year token")
	ErrNoMonth     = errors.New("pattern must contain a month token")
	ErrNoDay       = errors.New("pattern must contain a day token")
	ErrDuplicate   = errors.New("pattern repeats a field")
	ErrMismatch    = errors.New("input does not match the date format")
)
View Source
var Presets = []string{
	"DD/MM/YYYY",
	"MM/DD/YYYY",
	"YYYY-MM-DD",
	"D MMM YYYY",
	"MMM D, YYYY",
}

Presets are the patterns offered in the UI, in display order. Auto is presented alongside these but is not itself a pattern.

Functions

func Normalize

func Normalize(pattern string) string

Normalize returns pattern when it is valid and Auto when it is not, so a bad value stored by an older version degrades to the built-in rendering instead of breaking every date on the page.

func Parse

func Parse(s, pattern string, loc *time.Location) (time.Time, error)

Parse reads s as a date written in pattern, in loc. Leading and trailing space is ignored; the rest must match exactly.

func Placeholder

func Placeholder(pattern string) string

Placeholder renders a human-readable hint for an input expecting pattern, e.g. "dd/mm/yyyy hh:mm". Auto returns "".

func Render

func Render(t time.Time, pattern string) string

Render formats t with pattern. An empty or invalid pattern returns "" so callers can fall back to their own layout.

func Validate

func Validate(pattern string) error

Validate reports whether pattern is usable as a user's date format. Auto is valid and means "no pattern".

func WithTime

func WithTime(pattern string) string

WithTime returns the datetime pattern for a date pattern: the user's date format followed by a 24h clock. Auto stays Auto.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL