Created
December 15, 2011 17:27
-
-
Save flesch/1481970 to your computer and use it in GitHub Desktop.
str_to_time / time_to_str
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
# 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) |
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
# 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