Posted By

robksawyer on 03/03/09


Tagged

time words twitter nicetime


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

dx0ne
vip4lyf
bazjapan
TrentSkunk


TimeAgoInWords


 / Published in: ActionScript 3
 

  1. /**
  2. * Handles converting the time to a nicename like "2 hours ago"
  3. * @param datetime
  4. * @param display_recent
  5. * @param format
  6. * @param recent_threshold
  7. * @param backwards
  8. */
  9. static public function timeAgoInWords(date:String="1970-01-22",time:String="00:00:00",timezone:Number = -8 , format:String = "mm/dd/yy", backwards:Boolean = false, recent_threshold:Number = 60):String{
  10.  
  11. /**
  12. * Take special care to notice that month (4) is zero-based, meaning that January is 0, and December is 11.
  13. * Hour, the 4th parameter, starts with 0 as midnight, and ends with 23 as 11pm. The constructor is in the format
  14. * new Date(year, month, day, hours, minutes, seconds, milliseconds).
  15. */
  16. //Parse the date
  17. trace("========= DATE ========== ");
  18. trace(" "+ date);
  19. var dateArray:Array = date.split("-",3);
  20. var dYear:Number = Number(dateArray[0]);
  21. var dMonth:Number = Number(dateArray[1])-1;
  22. var dDay:Number = Number(dateArray[2]);
  23. //
  24. trace(" year: " + dateArray[0]);
  25. trace(" month: " + dateArray[1]);
  26. trace(" day: " + dateArray[2]);
  27.  
  28. var timeArray:Array = time.split(":");
  29. var dMin:Number = Number(timeArray[1]);
  30. var dHour:Number = Number(timeArray[0]);
  31. var meridian:String;
  32. if(dHour > 23){
  33. meridian = "PM";
  34. dHour = 12;
  35. }else{
  36. meridian = "AM";
  37. }
  38. //dHour %= 24;
  39. var dSec:Number = Number(timeArray[2]);
  40. trace(" min: " + dMin);
  41. trace(" hour: " + dHour);
  42. trace(" sec: " + dSec);
  43.  
  44. var datetime:Date = new Date();
  45. trace(" timezone: " + timezone);
  46. //datetime = DateUtility.ChangeTimezone(datetime, timezone);
  47. datetime.setUTCFullYear(dYear,dMonth,dDay);
  48. datetime.setUTCHours(dHour,dMin,dSec,0);
  49.  
  50. //var datetime:Date = new Date(dMonth+"/"+dDay+"/"+dYear+" "+time + " "+meridian);
  51. trace(" "+datetime+" ");
  52. trace("========================= ");
  53. var in_seconds:Number = datetime.getTime()/1000; //Get the Date in seconds
  54. var now:Date = new Date();
  55. var diff:Number;
  56. var months:Number;
  57. var weeks:Number;
  58. var days:Number;
  59. var hours:Number;
  60. var minutes:Number;
  61. var seconds:Number;
  62.  
  63. //var secondsSince:Number = Math.floor(now.time); //The number of seconds since midnight January 1, 1970, universal time, for a Date object.
  64. //trace(" Seconds since January 1, 1970 -> " + secondsSince);
  65. var secondsSince:Number = now.getTime()/1000; //Get the Date in seconds
  66. if (backwards) {
  67. diff = in_seconds - secondsSince;
  68. } else {
  69. diff = secondsSince - in_seconds;
  70. }
  71.  
  72. var dif:Number = diff;
  73. months = Math.floor(dif / 2419200);
  74. dif -= months * 2419200;
  75. weeks = Math.floor(dif / 604800);
  76. dif -= weeks * 604800;
  77. days = Math.floor(dif / 86400);
  78. dif -= days * 86400;
  79. hours = Math.floor(dif / 3600);
  80. dif -= hours * 3600;
  81. minutes = Math.floor(dif / 60);
  82. dif -= minutes * 60;
  83. seconds = dif;
  84.  
  85. var old:Boolean;
  86. if (months > 0) {
  87. // over a month old, just show date (mm/dd/yyyy format)
  88. //trace("format: " + format);
  89. var relative_date:String = 'on ' + formatTo(datetime,format);
  90. trace("=== FORMAT -> " + formatTo(datetime,format));
  91. old = true;
  92. } else {
  93. relative_date = '';
  94. old = false;
  95.  
  96. if (weeks > 0) {
  97. // weeks and days
  98. relative_date += (relative_date ? ', ' : '') + weeks + ' week' + (weeks > 1 ? 's' : '');
  99. relative_date += days > 0 ? (relative_date ? ', ' : '') + days + ' day' + (days > 1 ? 's' : '') : '';
  100. } else if(days > 0) {
  101. // days and hours
  102. relative_date += (relative_date ? ', ' : '') + days + ' day' + (days > 1 ? 's' : '');
  103. relative_date += hours > 0 ? (relative_date ? ', ' : '') + hours + ' hour' + (hours > 1 ? 's' : '') : '';
  104. } else if(hours > 0) {
  105. // hours and minutes
  106. relative_date += (relative_date ? ', ' : '') + hours + ' hour' + (hours > 1 ? 's' : '');
  107. relative_date += minutes > 0 ? (relative_date ? ', ' : '') + minutes + ' minute' + (minutes > 1 ? 's' : '') : '';
  108. } else if(minutes > 0) {
  109. // minutes only
  110. relative_date += (relative_date ? ', ' : '') + minutes + ' minute' + (minutes > 1 ? 's' : '');
  111. } else {
  112. // seconds only
  113. relative_date += (relative_date ? ', ' : '') + Math.round(seconds) + ' second' + (seconds != 1 ? 's' : '');
  114. }
  115. }
  116.  
  117. var ret:String = relative_date;
  118.  
  119. // show relative date and add proper verbiage
  120. if (!backwards && !old) {
  121. ret += ' ago';
  122. }
  123. /*if(display_recent) {
  124. if(dif<=recent_threshold) {
  125. ret = '<span class="recent">'+ret+'</span>';
  126. }
  127. }*/
  128.  
  129. return ret;
  130. //return formatTo(datetime,format) + " " +time;
  131. }

Report this snippet  

You need to login to post a comment.