Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Last active August 7, 2025 09:30
Show Gist options
  • Select an option

  • Save wjkoh/ebc6e8fb852608dd4df31af773bb39d2 to your computer and use it in GitHub Desktop.

Select an option

Save wjkoh/ebc6e8fb852608dd4df31af773bb39d2 to your computer and use it in GitHub Desktop.
Go: How to Parse HH:MM:SS and HH:MM:SS.ttt
func ParseHMS(hms string) (time.Duration, error) {
fields := strings.Split(strings.TrimSpace(hms), ":")
var s string
switch len(fields) {
case 2:
s = fields[0] + "m" + fields[1] + "s"
case 3:
s = fields[0] + "h" + fields[1] + "m" + fields[2] + "s"
default:
return time.Duration(0), fmt.Errorf("parse h:m:s: %s", hms)
}
return time.ParseDuration(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment