Life as Clay

Posts Tagged ‘duration

Javascript snippet to display elapsed time in words

leave a comment »

Send in two strings in the format indicated and return a string that represents the time elapsed between the two inputs, in words.

function findDuration(startTime, endTime) {
	
	// Accepts input of two time stamps in this format:
	// 2011-11-22T12:40:00-05:00
	
	var year_start    	= parseInt(startTime.substring(0,4), 10);
	var month_start   	= parseInt(startTime.substring(5,7), 10);
	var day_start     	= parseInt(startTime.substring(8,10), 10);
	var hours_start   	= parseInt(startTime.substring(11,13), 10);
	var minutes_start 	= parseInt(startTime.substring(14,16), 10);
	
	var year_end    	= parseInt(endTime.substring(0,4), 10);
	var month_end   	= parseInt(endTime.substring(5,7), 10);
	var day_end     	= parseInt(endTime.substring(8,10), 10);
	var hours_end   	= parseInt(endTime.substring(11,13), 10);
	var minutes_end 	= parseInt(endTime.substring(14,16), 10);
	
    var years_total    	= 0;
    var months_total   	= 0;
    var days_total     	= 0;
    var hours_total    	= 0;
    var minutes_total  	= 0;
    
    var earlier_date = new Date();
    earlier_date.setFullYear(year_start);
    earlier_date.setMonth(month_start - 1);
    earlier_date.setDate(day_start);
    earlier_date.setHours(hours_start);
    earlier_date.setMinutes(minutes_start);
    
    var later_date = new Date();
    later_date.setFullYear(year_end);
    later_date.setMonth(month_end - 1);
    later_date.setDate(day_end);
    later_date.setHours(hours_end);
    later_date.setMinutes(minutes_end);
    
    var nTotalDiff 	= later_date.getTime() - earlier_date.getTime();
    var oDiff 		= new Object();
    
    oDiff.days 		= Math.floor(nTotalDiff/1000/60/60/24);
    nTotalDiff 	   -= oDiff.days*1000*60*60*24;
    
    oDiff.hours 	= Math.floor(nTotalDiff/1000/60/60);
    nTotalDiff 	   -= oDiff.hours*1000*60*60;
    
    oDiff.minutes 	= Math.floor(nTotalDiff/1000/60);
    nTotalDiff 	   -= oDiff.minutes*1000*60;
    
    oDiff.seconds 	= Math.floor(nTotalDiff/1000);
    
    oDiff.years 	= 0;
    if (oDiff.days > 364) {
        oDiff.years = Math.floor(oDiff.days / 365);
        oDiff.days 	= oDiff.days - (oDiff.years * 365) - 1;
    }
    
    var diff_string = '';
    if (oDiff.years > 0) {
        if (oDiff.years == 1) {
            diff_string += oDiff.years + " year ";
        } else {
            diff_string += oDiff.years + " years ";
        }
    }
    if (oDiff.days > 0) {
        if (oDiff.days == 1) {
            diff_string += oDiff.days + " day ";
        } else {
            diff_string += oDiff.days + " days ";
        }
    }
    if (oDiff.hours > 0) {
        if (oDiff.hours == 1) {
            diff_string += oDiff.hours + " hour ";
        } else {
            diff_string += oDiff.hours + " hours ";
        }
    }
    if (oDiff.minutes > 0) {
        if (oDiff.minutes == 1) {
            diff_string += oDiff.minutes + " minute ";
        } else {
            diff_string += oDiff.minutes + " minutes";
        }
    }

    // Add support for seconds here, if you want it.
    
    return diff_string;
}
Advertisements

Written by Clay

November 25, 2011 at 09:58

Posted in Code, javascript

Tagged with , ,