2

I need to make a calculation of passed and remaining time of an operation in C#. I have the start of the operation saved in a string format of HH:MM:SS

I have a default time length of the operation in a string format of HH:MM:SS

Now I would like to calculate:

  • The remaining time / extra time: For example if the operation is still below the default length, it should display -HH:MM:SS, and if the operation took longer than the default time, it should display +HH:MM:SS
  • If the operation took longer, I would also like to have a double value of HH,MM in % style. For example: 3hours and 30 minutes should be displayed as 3,5

Both results to be displayed next to each other. I know I have to translate the string values into DateTime and/or TimeSpan values to do calculations, but currently I have no idea how to calculate since the first operation for example would not give me a negative value, but just get back in time [22:30:00 of yesterday].

2
  • You save valuers as DateTime, but calculations result in TimeSpans, as the names suggest. Why do you keep the time as string, just leave it DateTime instead of converting all the time? Commented May 12, 2017 at 7:06
  • I get the values from an external web page, so the values come into the program as string in the first place. Commented May 12, 2017 at 7:11

2 Answers 2

9

Try this..

var start = "17:05:11"; // Pass this as a parameter
var startTime = DateTime.Parse(start);

var defaultDuration = TimeSpan.FromMinutes(2);
TimeSpan operationDuration = startTime - DateTime.Now;

TimeSpan diff = defaultDuration - operationDuration;

if (operationDuration > defaultDuration)
{
    Console.Out.WriteLine($"+{diff.Hours}:{diff.Minutes}:{diff.Seconds}");
}
else
{
    Console.Out.WriteLine($"-{diff.Hours}:{diff.Minutes}:{diff.Seconds}");
    Console.Out.WriteLine($"{diff.Hours},{Math.Round(((double)(diff.Minutes * 100 / 60)),0, MidpointRounding.AwayFromZero)}");//example: 3hours and 30 minutes should be displayed as 3,5
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had to adapt some small things [TimeSpan.FromMinutes()] but in general your proposed solution works. Thanks alot!
2

At first save the default time as TimeSpan. Then you can take DateTime.Now and save it when the operation starts. Take another DateTime.Now later when it finished. After this point you can calculate the TimeSpan for the current operation. Then you can calculate the difference from these two TimeSpans as another TimeSpan. It can be positive or negativ and with these values you can do whatever you want.

TimeSpan defaultDuration = new TimeSpan(3, 30, 0);
DateTime begin = DateTime.Now;

//Do some work

DateTime end = DateTime.Now;
TimeSpan thisDuration = end - begin;
Console.WriteLine("Default: " + defaultDuration.ToString("hh\\:mm\\:ss"));
Console.WriteLine("This time: " + thisDuration.ToString("hh\\:mm\\:ss"));
Console.Write("Difference: ");
if (thisDuration > defaultDuration)
    Console.Write("-");
Console.WriteLine((thisDuration - defaultDuration).ToString("hh\\:mm\\:ss"));

2 Comments

Thanks for the suggestion, but I can't use "begin" like this. I get the value from an external web page as string.
@kai search for DateTime.Parse

Your Answer

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