Posted By

arpit on 07/23/09


Tagged

date


Versions (?)

ActionScript function for turning a date into a time interval


 / Published in: ActionScript 3
 

URL: http://blogs.adobe.com/cantrell/archives/2009/06/actionscript-fu.html

  1. private function formatDate(d:Date):String
  2. {
  3. var now:Date = new Date();
  4. var diff:Number = (now.time - d.time) / 1000; // convert to seconds
  5. if (diff < 60) // just posted
  6. {
  7. return "Just posted";
  8. }
  9. else if (diff < 3600) // n minutes ago
  10. {
  11. return (Math.round(diff / 60) + " minutes ago");
  12. }
  13. else if (diff < 86400) // n hours ago
  14. {
  15. return (Math.round(diff / 3600) + " hours ago");
  16. }
  17. else // n days ago
  18. {
  19. return (Math.round(diff / 86400) + " days ago");
  20. }
  21. }

Report this snippet  

You need to login to post a comment.