Last active
May 23, 2020 01:58
-
-
Save spatialtime/a32684ce37e2461a50985e203360ee00 to your computer and use it in GitHub Desktop.
Golang and ISO 8601 ordinal dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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