TimeSpan.TotalMinutes without seconds
Asked Answered
D

3

9

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

?

Decollate answered 11/7, 2012 at 11:2 Comment(0)
M
9

Just explicitly convert the result to int:

var minutesPassed = (int)(DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes;
Medication answered 11/7, 2012 at 11:6 Comment(3)
Converting 254.54445556 to an int wouldn't round up?Polymer
@Curt no :) it will give you 254.Odelle
@Curt I think Eren is right. The float will be converted to int. This is that I need.Decollate
P
7

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

var minutesPassed = Math.Floor((DateTime.UtcNow - conversionsList.Last().DateStamp).TotalMinutes);
Polymer answered 11/7, 2012 at 11:4 Comment(0)
A
4

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

Alcaide answered 11/7, 2012 at 11:6 Comment(2)
@Curt, it won't, I tried this in Visual studio and it is giving 254. double d = 254.54445556d; int test = (int)d; Correct me if my test is wrongAlcaide
Cheers I always thought it rounded to the nearest int. Surprised!Polymer

© 2022 - 2024 — McMap. All rights reserved.