76

To get TimeSpan in minutes from given two Dates I am doing the following

int totalMinutes = 0;
TimeSpan outresult = end.Subtract(start);
totalMinutes = totalMinutes + ((end.Subtract(start).Days) * 24 * 60) + ((end.Subtract(start).Hours) * 60) +(end.Subtract(start).Minutes);
return totalMinutes;

Is there a better way?

5 Answers 5

192
TimeSpan span = end-start;
double totalMinutes = span.TotalMinutes;
22

Why not just doing it this way?

DateTime dt1 = new DateTime(2009, 6, 1);
DateTime dt2 = DateTime.Now;
double totalminutes = (dt2 - dt1).TotalMinutes;

Hope this helps.

12

I would do it like this:

int totalMinutes = (int)(end - start).TotalMinutes;
1
  • 3
    Really old but cast will truncate. Use Convert.ToInt32 to get correct rounding.
    – Asken
    Commented Apr 20, 2017 at 13:52
10
double totalMinutes = (end-start).TotalMinutes;
7

See TimeSpan.TotalMinutes:

Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.