Sreedhar
Sreedhar

Reputation: 30015

How do I get TimeSpan in minutes given two Dates?

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?

Upvotes: 76

Views: 138383

Answers (5)

Will
Will

Reputation:

double totalMinutes = (end-start).TotalMinutes;

Upvotes: 10

David Božjak
David Božjak

Reputation: 17607

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.

Upvotes: 22

Luke Quinane
Luke Quinane

Reputation: 16605

I would do it like this:

int totalMinutes = (int)(end - start).TotalMinutes;

Upvotes: 12

Josh
Josh

Reputation: 69242

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

Upvotes: 192

Anton Gogolev
Anton Gogolev

Reputation: 115691

See TimeSpan.TotalMinutes:

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

Upvotes: 7

Related Questions