TimeSpan.TotalMinutes without seconds

I am using code

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

to calculate how many minutes passed between two dates. The result that I get looks like

254.54445556

I get minutes and seconds. How to get a result that will contain only minutes, such as

254

?

+5
source share
3 answers

Just explicitly convert the result to int:

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

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

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

You can just get the int part

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

this will give you the inside of the meaning.

EDIT: how much rounding of the value is considered. It is not true. Consider the following:

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

It testwill contain 254, not255

The only problem with explicit OverFlowException application

+3
source

All Articles