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 ¶
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.
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 ¶
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") )
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 ¶
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 ¶
Parse reads s as a date written in pattern, in loc. Leading and trailing space is ignored; the rest must match exactly.
func Placeholder ¶
Placeholder renders a human-readable hint for an input expecting pattern, e.g. "dd/mm/yyyy hh:mm". Auto returns "".
func Render ¶
Render formats t with pattern. An empty or invalid pattern returns "" so callers can fall back to their own layout.
Types ¶
This section is empty.