Skip to content

Instantly share code, notes, and snippets.

@flesch
Created December 15, 2011 17:27
Show Gist options
  • Save flesch/1481970 to your computer and use it in GitHub Desktop.
Save flesch/1481970 to your computer and use it in GitHub Desktop.
str_to_time / time_to_str
# Convert string to a timestamp.
# str_to_time("01:20") = 80
str_to_time = (str) ->
str.replace /^([0-9]{1,2}):([0-9]{2})$/, (t, m, s) -> (m* 60) + parseFloat(s)
# Convert a timestamp to a string.
# time_to_str(80) = "01:20"
time_to_str = (time) ->
exp = /[0-9]{2}$/
m = exp.exec('0' + Math.floor(time / 60))
s = exp.exec('0' + Math.floor(time % 60))
m.concat(s).join(':')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment