0

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.

6
  • What language are you using? We'll definitely need to know that before moving forward. Commented Apr 28, 2017 at 17:57
  • 4
    Possible duplicate of Convert string to Time Commented Apr 28, 2017 at 17:58
  • 2
    Is it an elapsed time value, such as you might measure in real life using a stopwatch? Or is it a time of day value, such as you might observe in real life on a clock? Those are two very different things. (Note, the dup Avitus points at is for the latter.) Commented Apr 28, 2017 at 18:03
  • 1
    @MattJohnson it's an elapsed time. I'm getting these values from an XML node, thus in the string format. Is there anyway to convert string to time? Commented Apr 28, 2017 at 18:05
  • @Avitus, it's not a duplicate because you cannot easily parse that string into TimeSpan or DateTime. Commented Apr 28, 2017 at 18:12

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

7 Comments

Well, if you aren't going to evaluate the result of the conversion, use ParseExact, not TryParseExact.
Fixed your invocation for you. :) But the addition is correct and gives the correct value.
@MattJohnson How would you take an average of the timespan then?
@AnmolJaising TimeSpan avg = new TimeSpan(result.Ticks / numOfTimes);
@Tim: Thanks for all your help :D
|
0

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

5 Comments

I want to add them because i have a code extracting time from an XML running on 3 machines. So wanted to add those time and get the avg
Ok then I'd just convert them to DateTime > Milliseconds > add together than average out.
Do not use DateTime unless the times represent time of day. Use TimeSpan
Your point about adding times together is why I asked what the time represents. You certainly can add 1 minute + 1 minute = 2 minutes. However, you cannot add 3 o'clock am + 5 o'clock pm because that's nonsense. :)
@MattJohnson , agreed. You can add periods of time, but not two different times.
0

If 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.

2 Comments

Why do all that parsing when there's TimeSpan.ParseExact?
@juharr, glad you asked. That's because when I tried TimeSpan.ParseExact I didn't escape periods and it didn't work for me. And then Tim posted his answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.