Time Sheet

I write my time in Evernote, and needed a way to parse it for invoicing Format is: July 2nd, 2013 12:54pm - 2:30pm //Note about line Does not handle day overlaps such as 11:30pm - 2:30am

by senica

HTML

<textarea style="width:100%; height:300px;"></textarea>
<div id="totalHours"></div>
<div id="result"></div>

JavaScript

$("textarea").on("change", function(){

    var result = $("#result");
    result.html("");

    var total = 0;
    var minitotal = 0;

    var base = $("textarea").val();
    
    $('<div><h1>Length: '+ base.length +'</h1></div>').appendTo("#totalHours");
    
    var lines = base.split("\n");
    $.each(lines, function(i, line){
        var matches = line.match(/(\d+:\d{2}[a|p]m).*?(\d+:\d{2}[a|p]m)(.*)/);
        if(matches){            
            var start = matches[1];
            var hold = start.split(":");
            var hour = hold[0];
            var meridiem = hold[1].substr(-2);
            var minutes = hold[1].substr(0,2);
            hour = meridiem == "pm" && parseInt(hour) != 12 ? parseInt(hour) + 12 : hour;
            
            start = new Date(2013, 8, 1, hour, minutes, 0, 0).getTime();
            
            
            var end = matches[2];
            var hold = end.split(":");
            var hour = hold[0];
            var meridiem = hold[1].substr(-2);
            var minutes = hold[1].substr(0,2);
            hour = meridiem == "pm" && parseInt(hour) != 12 ? parseInt(hour) + 12 : hour;
            
            end = new Date(2013, 8, 1, hour, minutes, 0, 0).getTime();
            
            var milliseconds = end - start;
            
            //Add to global total
            total = total + milliseconds;
            minitotal = minitotal + milliseconds;
            
            var seconds = ('00' + parseInt((milliseconds / 1000) % 60).toString()).substr(-2);
            var minutes = ('00' + parseInt((milliseconds / (1000*60)) % 60).toString()).substr(-2);
            var hours   = ('00' + parseInt((milliseconds / (1000*60*60)) % 24).toString()).substr(-2);
            
            $('<div><strong>'+hours + ':' + minutes + ':' + seconds +'</strong> '+matches[0]+'</div>').appendTo(result);
        }else{
            
            var seconds = ('00' + parseInt((minitotal / 1000) % 60).toString()).substr(-2);
           ...