summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorByron Jones <glob@mozilla.com>2015-05-27 05:57:29 +0200
committerByron Jones <glob@mozilla.com>2015-05-27 05:57:29 +0200
commit35aab8c392ac6ad404bb0d902cca6b50480072da (patch)
tree035f26e0c3151fbd088b07c6e8140ce6412c5f2e /js
parent087f74073fdc81ec5707f2989c99b34770e864eb (diff)
downloadbugzilla-35aab8c392ac6ad404bb0d902cca6b50480072da.tar.gz
bugzilla-35aab8c392ac6ad404bb0d902cca6b50480072da.tar.xz
Bug 1158010: provide a standard and simple way to render relative dates, in perl and javascript
Diffstat (limited to 'js')
-rw-r--r--js/util.js27
1 files changed, 27 insertions, 0 deletions
diff --git a/js/util.js b/js/util.js
index 78b64b516..c27dc2b11 100644
--- a/js/util.js
+++ b/js/util.js
@@ -341,3 +341,30 @@ function bz_toggleClass(anElement, aClass) {
YAHOO.util.Dom.addClass(anElement, aClass);
}
}
+
+/* Retruns a string representation of a duration.
+ *
+ * @param ss Duration in seconds
+ * or
+ * @param date Date object
+ */
+function timeAgo(param) {
+ var ss = param.constructor === Date ? Math.round((new Date() - param) / 1000) : param;
+ var mm = Math.round(ss / 60),
+ hh = Math.round(mm / 60),
+ dd = Math.round(hh / 24),
+ mo = Math.round(dd / 30),
+ yy = Math.round(mo / 12);
+ if (ss < 10) return 'just now';
+ if (ss < 45) return ss + ' seconds ago';
+ if (ss < 90) return 'a minute ago';
+ if (mm < 45) return mm + ' minutes ago';
+ if (mm < 90) return 'an hour ago';
+ if (hh < 24) return hh + ' hours ago';
+ if (hh < 36) return 'a day ago';
+ if (dd < 30) return dd + ' days ago';
+ if (dd < 45) return 'a month ago';
+ if (mo < 12) return mo + ' months ago';
+ if (mo < 18) return 'a year ago';
+ return yy + ' years ago';
+}