Tomas
Tomas

Reputation: 18097

TimeSpan.TotalMinutes without seconds

I am using the code

var minutesPassed = (DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;

to calculate how much minutes passed between two dates. The result which I get looks like

254.54445556

I get minutes and seconds. How to get result which would contain only minutes like this

254

?

Upvotes: 9

Views: 17338

Answers (3)

Habib
Habib

Reputation: 223237

You can just get the int part

int minutes = (int) (DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;

this will get you the int part of the value.

EDIT: as far as rounding of value is considered. That is not true. Consider the following:

 double d = 254.99999999999d;
 int test = (int)d;

Here test will hold 254, not 255

The only problem with the explicit cast is OverFlowException

Upvotes: 4

Curtis
Curtis

Reputation: 103348

Use Math.Floor() to convert 254.xxxx to 254:

var minutesPassed = Math.Floor((DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes);

Upvotes: 7

Eren Ersönmez
Eren Ersönmez

Reputation: 39085

Just explicitly convert the result to int:

var minutesPassed = (int)(DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;

Upvotes: 9

Related Questions