I have time "00.05.415" (mm:ss.000) which is in string format.
I want to convert it to a TIME format where I can add multiple times such as "00.05.415"+"00.06.415"+"00.07.415" to get one single added time.
I have time "00.05.415" (mm:ss.000) which is in string format.
I want to convert it to a TIME format where I can add multiple times such as "00.05.415"+"00.06.415"+"00.07.415" to get one single added time.
You'll want to use TimeSpan.ParseExact so you can specify the format that the time is in and then you can add the time spans together:
public static void Main(string[] args)
{
TimeSpan span1 = Convert("00.05.415");
TimeSpan span2 = Convert("00.07.415");
TimeSpan result = span1 + span2;
Console.WriteLine(result);
Console.ReadKey();
}
public static TimeSpan Convert(string span)
{
return TimeSpan.ParseExact(span, @"mm\.ss\.fff", CultureInfo.InvariantCulture);
}
https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx
ParseExact, not TryParseExact.You'd want to take a look at the link that Avitus has and has the one here.
I'll have to ask why you'd want to add them together. You can't really 'add' times together .
One possible options is to to convert each to milliseconds and then formatting the resultant value.
5 seconds + 3.2 seconds could be:
5000 + 3200 = 8200.
You'd then use System.TimeSpan to convert that into days, hours, minutes ...etc
thanks for the correction Matt
DateTime unless the times represent time of day. Use TimeSpanIf you know that the format is predefined to be mm:ss.000 you can do following to parse to TimeSpan:
var strings = "00:05.415".Split(new []{'.', ':'}, StringSplitOptions.RemoveEmptyEntries);
var minutes = int.Parse(strings[0]);
var seconds = int.Parse(strings[1]);
var milliseconds = int.Parse(strings[2]);
var time = new TimeSpan(0, 0, minutes, seconds, milliseconds);
And then you can add TimeSpans together.
TimeSpan.ParseExact?TimeSpan.ParseExact I didn't escape periods and it didn't work for me. And then Tim posted his answer.If your format is not predefined use this 4KB script https://github.com/alekspetrov/time-input-js
It converts string/number into time format HH:MM:SS
Examples
00:00:00 -> 00:00:00
12:01 -> 12:01:00
12 -> 12:00:00
25 -> 00:00:00
12:60:60 -> 12:00:00
1dg46 -> 14:06
["notatime"] -> 00:00:00 + console warn