Java 7, 164 bytes
String c(String a,String b){long s=x(b,1)-x(a,1)+(x(b,0)-x(a,0))*60,m=s%60;return(s/60)+":"+(m>9?m:"0"+m);}long x(String s,int i){return new Long(s.split(":")[i]);}
Explanation:
String c(String a, String b){ // Method with two String parameters and String return-type
long s = x(b,1) - x(a,1) // Get difference in seconds from input times
+ (x(b,0) - x(a,0)*60, // plus the difference in minutes times 60 to get the seconds
m = s%60; // Temp variable of seconds after we've subtracted the minutes (used multiple times)
return (s/60) // Return minutes
+":" // plus ":"
+(m>9?m:"0"+m); // plus seconds (with a leading 0 if necessary)
} // End of method
long x(String s,int i){ // Separate ethod with String and Integer parameters and long return-type
return new Long(s.split(":")[i]; // Return either minutes or seconds of String parameter based on the index
} // End of method
Test code:
Try it here.
class M{
String c(String a,String b){long s=x(b,1)-x(a,1)+(x(b,0)-x(a,0))*60,m=s%60;return(s/60)+":"+(m>9?m:"0"+m);}long x(String s,int i){return new Long(s.split(":")[i]);}
public static void main(String[] a){
M m = new M();
System.out.println(m.c("0:00", "0:01"));
System.out.println(m.c("0:55", "1:00"));
System.out.println(m.c("1:45", "3:15"));
}
}
Output:
0:01
0:05
1:30
:
is a command (data as code philosophy). Am I allowed to use spaces instead or do I need to find other language to answer this? \$\endgroup\$01:30
valid output? (leading zero) \$\endgroup\$