-1

I have two concatenated values such as

string BreakOut = "10:15 Mintes";
string BreakIn = "10:30 Minutes"; 

I want result as

Total=15 Minutes

It is a time difference calculation. But values are not in a time format it is in a string format. How can I do this. Please suggest a way. I tried like

 TimeSpan span=Convert.ToDateTime(BreakOut ).Subtract(Convert.ToDateTime(BreakIn ));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Semil Sebastian
  • 111
  • 1
  • 5
  • 18

3 Answers3

1

There is no point to parse them to DateTime since they are time interval. TimeSpan would be better choice since it is exactly what this for.

If Mintes is typo and your values are always the same standard format, you can easily split them with white space and then parse to TimeSpan.

For example;

var BreakOut= "10:15 Minutes";
var BreakIn = "10:30 Minutes";

BreakOut = BreakOut.Split(new string[]{" "},
                          StringSplitOptions.RemoveEmptyEntries)[0];
BreakIn = BreakIn.Split(new string[] { " " },
                        StringSplitOptions.RemoveEmptyEntries)[0];

var ts1 = TimeSpan.Parse(BreakOut, CultureInfo.InvariantCulture);
var ts2 = TimeSpan.Parse(BreakIn, CultureInfo.InvariantCulture);

var difference = ts2 - ts1;
Console.WriteLine("{0} minutes", difference.TotalMinutes); // 15 Minutes

Here a demonstration.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • @SemilSebastian Since `difference` is a `TimeSpan` it is too normal to see it like `00:15:00`. That means your `difference` has `0` hour, `15` minutes and `0` seconds. You can use it's `TotalMinutes` or `Minutes` properties to get `15` as a double or integer. [`TotalMinutes` property](https://msdn.microsoft.com/en-us/library/system.timespan.totalminutes%28v=vs.110%29.aspx) returns it as a `double` , [`Minutes` property](https://msdn.microsoft.com/en-us/library/system.timespan.minutes%28v=vs.110%29.aspx) returns it as an `int`. – Soner Gönül Feb 27 '15 at 08:49
0

if your values always refers to the same day, you can use something like that :

string BreakOut="10:15" ;
string BreakIn ="10:30"; 
double res =DiffInMinutes(BreakIn, BreakOut);
//Total=15 Minutes


        public static double DiffInMinutes(string _BreakIn, string _BreakOut)
        {
            double diff = 0;
            string[] BreakIn = _BreakIn.Split(':');
            string[] BreakOut = _BreakOut.Split(':');
            //timeSpan instanciated with Hours/minutes/seconds
            TimeSpan TimeElapsed = new TimeSpan(int.Parse(BreakOut[0]) - int.Parse(BreakIn[0]), int.Parse(BreakOut[1]) - int.Parse(BreakIn[1]), 0);

            diff = TimeElapsed.TotalMinutes;
            return diff;
        }
Emmanuel M.
  • 441
  • 6
  • 11
0

I downvoted your question, but maybe you really dont know that there is TimeSpan.Parse

string breakOut = "10:15 Minutes";
string breakIn = "10:30 Minutes";
string cleanBreakIn = breakIn.Split(' ')[0];
string cleanBreakOut = breakOut.Split(' ')[0];
TimeSpan total = TimeSpan.Parse(cleanBreakIn) - TimeSpan.Parse(cleanBreakOut);
Szer
  • 3,426
  • 3
  • 16
  • 36