Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ezhov-da/0208da012139b838cf1cf3720040d6b9 to your computer and use it in GitHub Desktop.
Save ezhov-da/0208da012139b838cf1cf3720040d6b9 to your computer and use it in GitHub Desktop.
отображение прошедшего времени
// ==> https://www.sencha.com/forum/showthread.php?98083-Ext-util-Format-elapsedTime
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(', ');
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment