Time.coffee 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class Time
  2. since: (time) ->
  3. now = +(new Date)/1000
  4. secs = now - time
  5. if secs < 60
  6. back = "Just now"
  7. else if secs < 60*60
  8. back = "#{Math.round(secs/60)} minutes ago"
  9. else if secs < 60*60*24
  10. back = "#{Math.round(secs/60/60)} hours ago"
  11. else if secs < 60*60*24*3
  12. back = "#{Math.round(secs/60/60/24)} days ago"
  13. else
  14. back = "on "+@date(time)
  15. back = back.replace(/1 ([a-z]+)s/, "1 $1") # 1 days ago fix
  16. return back
  17. date: (timestamp, format="short") ->
  18. parts = (new Date(timestamp*1000)).toString().split(" ")
  19. if format == "short"
  20. display = parts.slice(1, 4)
  21. else
  22. display = parts.slice(1, 5)
  23. return display.join(" ").replace(/( [0-9]{4})/, ",$1")
  24. timestamp: (date="") ->
  25. if date == "now" or date == ""
  26. return parseInt(+(new Date)/1000)
  27. else
  28. return parseInt(Date.parse(date)/1000)
  29. # Get elistamated read time for post
  30. readtime: (text) ->
  31. chars = text.length
  32. if chars > 1500
  33. return parseInt(chars/1500)+" min read"
  34. else
  35. return "less than 1 min read"
  36. window.Time = new Time