Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

?

like image 264
Tomas Avatar asked Jul 11 '12 11:07

Tomas


3 Answers

Just explicitly convert the result to int:

var minutesPassed = (int)(DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;
like image 114
Eren Ersönmez Avatar answered Oct 23 '22 07:10

Eren Ersönmez


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

var minutesPassed = Math.Floor((DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes);
like image 7
Curtis Avatar answered Oct 23 '22 07:10

Curtis


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

like image 3
Habib Avatar answered Oct 23 '22 06:10

Habib