Drupal doesn't implement a JS version of format_interval()
; this is a rough (minimally tested) port:
Drupal.formatInterval = function(interval, granularity, langcode) {
granularity = typeof granularity !== 'undefined' ? granularity : 2;
langcode = typeof langcode !== 'undefined' ? langcode : null;
var units = {
'1 year|@count years': 31536000,
'1 month|@count months': 2592000,
'1 week|@count weeks': 604800,
'1 day|@count days': 86400,
'1 hour|@count hours': 3600,
'1 min|@count min': 60,
'1 sec|@count sec': 1
},
output = '';
for (var key in units) {
var keys = key.split('|');
var value = units[key];
if (interval >= value) {
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), keys[0], keys[1], {}, { langcode: langcode });
interval %= value;
granularity--;
}
if (granularity == 0) {
break;
}
}
return output.length ? output : Drupal.t('0 sec', {}, { langcode: langcode });
}
Some random results using the above (they seem to match up to the PHP function as expected):
- 3643 => 1 hour 43 sec
- 92900 => 1 day 1 hour
- 2592000 => 1 month
- 9331200 => 3 months 2 weeks
- 297605232 => 9 years 5 months
t
method is a Drupal text sanitizing and translating equivalent to thet()
PHP function from Drupal core. – Ayesh K May 20 at 17:02