Time.coffee 1016 B

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