Small script to calculate project time

There are many project management tools, which support timing each task. I don’t have or need that kind of sophistication. I just write up start and end times and then calculate the difference. Here is a small script for it:


jQuery(window).load(function() {
    var timeSum = 0;

    var processLine = function(elem) {
	    str = elem.split("-");

        if((str[0].match(/([0-9]{4})/)) && (str[1].match(/([0-9]{4})/))) {

            var int0 = parseInt(str[0], 10);
            var int1 = parseInt(str[1], 10);

            var h0 = Math.floor(int0/100);
            var h1 = Math.floor(int1/100);

            var m0 = int0%100;
            var m1 = int1%100;

            if(h0 > h1) {
                h1 += 24;
            }

            if(m0 > m1) {
                m1 += 60;
                h0 += 1;
            }

            return (h1 - h0) * 60 + m1 - m0;
        }
    };

    jQuery("input[type='button'].sumTimes").on("click", function() {

        var lines = jQuery("textarea.timeStamps").val().split("\n");
        jQuery.each(lines, function (index, elem) {
            timeSum += processLine(elem);
        });
        var hoursSum = Math.floor(timeSum/60);
        var minsSum = timeSum - hoursSum * 60;
        jQuery("div.sumOfTimes").text(hoursSum + " hour(s), " + minsSum + " minute(s)");

    });
});

Please add a new / next interval (like: 1620-2055) on a new line.


Result:

Leave a Reply

Your email address will not be published. Required fields are marked *