package models import ( "encoding/json" "fmt" "time" ) type Date [3]int func ParseDate(s string) (Date, error) { date, err := time.ParseInLocation("2006-01-02", s, time.UTC) if err != nil { return [3]int{}, err } y, m, d := date.Date() return Date{y, int(m), d}, nil } func (d *Date) UnmarshalJSON(b []byte) error { var str string err := json.Unmarshal(b, &str) if err != nil { return err } *d, err = ParseDate(str) if err != nil { return err } return nil } func (d Date) String() string { return fmt.Sprintf("%04d-%02d-%02d", d[0], d[1], d[2]) } func (d Date) MarshalJSON() ([]byte, error) { return []byte("\"" + d.String() + "\""), nil } func (d Date) ToTime() time.Time { return time.Date(d[0], time.Month(d[1]), d[2], 0, 0, 0, 0, time.UTC) }