Skip to content

Instantly share code, notes, and snippets.

@misterunix
Last active February 12, 2023 00:23
Show Gist options
  • Save misterunix/3ace3b5a7e3afb94ceb617193be0e2e2 to your computer and use it in GitHub Desktop.
Save misterunix/3ace3b5a7e3afb94ceb617193be0e2e2 to your computer and use it in GitHub Desktop.
Convert seconds into a readable time string.

Convert elapsed seconds into a nicely formatted string.

// Convert seconds into a readable time. This is not for real time, only for elapsed time. 
func TimeStr(sec int) (res string) {
	wks, sec := sec/604800, sec%604800
	ds, sec := sec/86400, sec%86400
	hrs, sec := sec/3600, sec%3600
	mins, sec := sec/60, sec%60
	res += fmt.Sprintf("%02dw:", wks)
	res += fmt.Sprintf("%02dd:", ds)
	res += fmt.Sprintf("%02dh:", hrs)
	res += fmt.Sprintf("%02dm:", mins)
	res += fmt.Sprintf("%02ds", sec)
	return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment