Skip to content

Instantly share code, notes, and snippets.

@spatialtime
Last active May 23, 2020 01:58
Show Gist options
  • Save spatialtime/a32684ce37e2461a50985e203360ee00 to your computer and use it in GitHub Desktop.
Save spatialtime/a32684ce37e2461a50985e203360ee00 to your computer and use it in GitHub Desktop.
Golang and ISO 8601 ordinal dates
// This snippet demonstrates Golang formatting and parsing of
// ISO 8601 ordinal dates (4-digit year + "-" + ordinal day).
// Note: an ordinal day in this context is the nth day of
// the year, with Jan 1 being ordinal day 1 and Dec 31
// of non-leap year (a "common year") being day 365.
import (
"time"
)
// FormatOrdinalDate returns an ISO 8601 ordinal date string.
// In this context, Ordinal date represents the nth day of the year.
func FormatOrdinalDate(date time.Time) string {
return date.Format("2006-002")
}
// ParseOrdinalDate parses an ISO 8601 string representing a ordinal date,
// and returns the resultant golang time.Time insance.
func ParseOrdinalDate(isoOrdinalDate string) (time.Time, error) {
return time.Parse("2006-002", isoOrdinalDate)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment