PDA

View Full Version : Ext.util.Format.elapsedTime



Ytorres
28 Apr 2010, 11:36 AM
Hi all,

Just a simple override for Ext.util.Format to display elapsed time between 2 dates, found here (http://www.labs.mimmin.com/countdown/) :



Ext.apply(Ext.util.Format, {

/**
* Return an elapsed time.
* Original script by http://www.labs.mimmin.com/countdown/
*
* @param dateStart A date object representing the start date
* @param dateEnd A date object representing the end date
* @param i18n An object to internationalise the result
* @param onlyLargestUnit Return only the largest unit. Default to false.
* @param hideEmpty Hide empty units. Default to true.
* @return The elapsed time
*/
elapsedTime: function(dateStart, dateEnd, i18n, onlyLargestUnit, hideEmpty) {

var seconds = Math.floor((dateEnd.getTime() - dateStart.getTime()) / 1000),
returnArray = [], value, unit, secondsConverted;

if (seconds < 1) {
return '';
}

if (typeof(hideEmpty) == 'undefined' || hideEmpty == null) {
hideEmpty = true;
}
if (typeof(onlyLargestUnit) == 'undefined' || onlyLargestUnit == null) {
onlyLargestUnit = false;
}
if (typeof(i18n) == 'undefined' || i18n == null) {
i18n = {
years : ['year', 'years'],
months : ['month', 'months'],
weeks : ['week', 'weeks'],
days : ['day', 'days'],
hours : ['hour', 'hours'],
minutes: ['minute', 'minutes'],
seconds: ['second', 'seconds']
};
}

var units = {
years : 12 *4*7*24*60*60,
months : 4 *7*24*60*60,
weeks : 7 *24*60*60,
days : 24*60*60,
hours : 60*60,
minutes: 60,
seconds: 1
};

for (unit in units) {
value = units[unit];
if (seconds / value >= 1 || unit == 'seconds' || !hideEmpty) {
secondsConverted = Math.floor(seconds / value);
var i18nUnit = i18n[unit][secondsConverted <= 1 ? 0 : 1];
returnArray.push(secondsConverted + ' ' + i18nUnit);
seconds -= secondsConverted * value;

if (onlyLargestUnit) {
break;
}
}
};

return returnArray.join(', ');

}


Usage example :



var startDate = new Date(2010,3,26, 21,0,0),
endDate = new Date(2011,3,26, 18,0,1),
i18nFrench= {
years : ['année', 'années'],
months : ['mois', 'mois'],
weeks : ['semaine', 'semaines'],
days : ['jour', 'jours'],
hours : ['heure', 'heures'],
minutes: ['minute', 'minutes'],
seconds: ['seconde', 'secondes']
},
p = Ext.util.Format.elapsedTime(startDate, endDate, i18nFrench, false, false);

console.info(p); // Display : 1 année, 1 mois, 0 semaine, 0 jour, 21 heures, 0 minute, 1 seconde

startDate = new Date(2010,3,26, 21,0,0),
endDate = new Date(2011,3,26, 18,0,1),
p = Ext.util.Format.elapsedTime(startDate, endDate);

console.info(p); // Display : 1 year, 1 month, 21 hours, 1 second

startDate = new Date(2010,3,26, 21,0,0),
endDate = new Date(2011,3,26, 18,0,1),
p = Ext.util.Format.elapsedTime(startDate, endDate, null, true, false);


console.info(p); // Display : 1 year


Best,
Yannick

Animal
28 Apr 2010, 11:21 PM
Why not just insert a new member into http://www.extjs.com/deploy/dev/docs/?class=Date&member=formatFunctions ?

Ytorres
29 Apr 2010, 2:35 AM
Hi Animal,

I just don't know this way.

I will rework on and post the change

Best,
Yannick

Animal
29 Apr 2010, 3:40 AM
like this?



(function() {
var oneHour = 1000 * 60 * 60,
oneDay = oneHour * 24,
dr = /(\d{2}):(\d{2}):(\d{2})/;

// Convert minutes to a time
Date.parseFunctions.minutes = function(minutes) {
if (Ext.isString(minutes)) {
minutes = parseInt(minutes, 10) || 0;
}
return new Date(minutes * 60 * 1000);
};

Date.parseFunctions.duration = function(s) {
var values = dr.exec(s);
if (values && values.length == 4) {
return new Date(1970, 0, 1, parseInt(values[1]), parseInt(values[2]), parseInt(values[3]))
}
};

Date.formatFunctions.duration = function() {
var d, m = this.getTime();
m -= (m % oneDay);
return ((d = m / oneDay) ? (d + ' ') : '') + this.format('H:i:s');
};

Date.formatFunctions.durationHMS = function() {
var d, m = this.getTime();
m -= (m % oneHour);
return (m / oneHour) + ':' + this.format('i:s');
};
})();

haddow777
13 Sep 2012, 12:57 PM
years : 12 *4*7*24*60*60,
months : 4 *7*24*60*60,

Doesn't that mean that all months have just 28 days in them? I think you will run into problems here unless you are wanting every month to be a non leap year February.