1. #1
    Ext User Ytorres's Avatar
    Join Date
    Jun 2007
    Location
    Rennes, France
    Posts
    285
    Vote Rating
    0
    Ytorres is on a distinguished road

      0  

    Default Ext.util.Format.elapsedTime

    Ext.util.Format.elapsedTime


    Hi all,

    Just a simple override for Ext.util.Format to display elapsed time between 2 dates, found here :

    PHP Code:
        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(dateStartdateEndi18nonlyLargestUnithideEmpty) {

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

                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 >= || unit == 'seconds' || !hideEmpty) {
                        
    secondsConverted Math.floor(seconds value);
                        var 
    i18nUnit i18n[unit][secondsConverted <= 1];
                        
    returnArray.push(secondsConverted ' ' i18nUnit);
                        
    seconds -= secondsConverted value;

                        if (
    onlyLargestUnit) {
                            break;
                        }
                    }
                };

                return 
    returnArray.join(', ');

            } 
    Usage example :

    PHP Code:
    var startDate = new Date(2010,3,2621,0,0),
        
    endDate   = new Date(2011,3,2618,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(startDateendDatei18nFrenchfalsefalse);

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

    startDate = new Date(2010,3,2621,0,0),
    endDate   = new Date(2011,3,2618,0,1),
    p         Ext.util.Format.elapsedTime(startDateendDate);

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

    startDate = new Date(2010,3,2621,0,0),
    endDate   = new Date(2011,3,2618,0,1),
    p         Ext.util.Format.elapsedTime(startDateendDatenulltruefalse);


    console.info(p); // Display : 1 year 
    Best,
    Yannick
    "Software is like sex: it's better when it's free" - Linus Torvald
    https://edit.php.net - A tool written with ExtJs for translated Php documentation

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Why not just insert a new member into http://www.extjs.com/deploy/dev/docs...ormatFunctions ?

  3. #3
    Ext User Ytorres's Avatar
    Join Date
    Jun 2007
    Location
    Rennes, France
    Posts
    285
    Vote Rating
    0
    Ytorres is on a distinguished road

      0  

    Default


    Hi Animal,

    I just don't know this way.

    I will rework on and post the change

    Best,
    Yannick
    "Software is like sex: it's better when it's free" - Linus Torvald
    https://edit.php.net - A tool written with ExtJs for translated Php documentation

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    like this?

    Code:
    (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');
        };
    })();

  5. #5
    Sencha User
    Join Date
    Mar 2012
    Posts
    18
    Vote Rating
    0
    haddow777 is on a distinguished road

      0  

    Default


    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.

Thread Participants: 2