Get delta string from two date values - Node.js Date

Node.js examples for Date:Date Format

Description

Get delta string from two date values

Demo Code

/**//from  w ww.j  a v  a 2s. c o m
 * [getTimeDeltaString description]
 * @param  {[type]} time1 [description]
 * @param  {[type]} time2 [description]
 * @return {[type]}       [description]
 */
function getTimeDeltaString(time1, time2) {
    var deltaMs = time1 - time2;
    var deltaS = Math.floor(deltaMs / 1000);
    if (deltaS < 60) {
        return deltaS + 's';
    } else if (deltaS < 60*60) {
        return Math.floor(deltaS / 60) + 'm';
    } else {
        return Math.floor(deltaS / (60 * 60)) + 'h';
    }
};

Related Tutorials