Skip to content

Instantly share code, notes, and snippets.

@pantoniotti
Last active December 18, 2015 00:19
Show Gist options
  • Save pantoniotti/5695654 to your computer and use it in GitHub Desktop.
Save pantoniotti/5695654 to your computer and use it in GitHub Desktop.
Some time methods in a coffeescript class
# Time Class
class @Time
constructor: (@seconds) ->
getElapsedTimeString: (seconds) ->
currentTimeString = "00:00:00"
try
hours = Math.floor(seconds / 3600)
seconds = seconds % 3600
minutes = Math.floor(seconds / 60)
seconds = seconds % 60
seconds = Math.floor(seconds)
# Pad the minutes and seconds with leading zeros, if required
hours = @padTime(hours)
minutes = @padTime(minutes)
seconds = @padTime(seconds)
# Compose the string for display
currentTimeString = hours + ":" + minutes + ":" + seconds
catch error
alert error.message
currentTimeString
# Add zeros to single digit time
padTime: (num) ->
((if num < 10 then "0" else "")) + num
# Converts time hh:mn:sc into minutes
timeToMinutes: (time) ->
total_mn = 0
try
hr = parseInt(time.substr(0, 2), 10)
mn = parseInt(time.substr(3, 2), 10)
ss = parseInt(time.substr(6, 2), 10)
total_mn = (hr * 60) + mn
total_mn = (if ss > 30 then total_mn + 1 else total_mn)
catch error
alert error.message
total_mn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment