Konstantinos-infogeek
4/10/2012 - 9:49 PM

javascript

javascript

//Created by Konstantinos Tsatsarounos
//For Infogeek.gr Tutorials
//description:
//it sums a string of minutes like:  '01:12,25:47,2:12' 	

function getTimeString(str){	
			var reg = new RegExp(/\d{1,2}\:\d{1,2}/), str = (str!='')? str.split(',') : false, seconds=0, pointer=0;
			function getTime(timeString){
				pointer++;
				if(reg.test(timeString)){
				time = timeString.split(':');
				seconds += parseInt(time[0],10)*60 + parseInt(time[1],10)
				return true;}
				else {alert('Type error, position: '+pointer); return 0;}
			};
			
				if(str) str.map(getTime); else alert('empty string');
			return ((seconds>3600)? Math.floor(seconds/3600):'00')+':'+((seconds%3600>60)? Math.floor((seconds%3600)/60):'00')+':'+((seconds%60>0)? seconds%60 :'00')
		}