Reputation: 51927
I want to calculate how much time elapsed between a datetime and now in minutes. This is what I have:
TimeSpan SinceStatusChange = new TimeSpan();
SinceStatusChange = (double)(DateTime.Now.Date - StatusDateTime).TotalMinutes;
Basically, I'm looking to write this conditional statement:
if (SinceStatusChange is greater than 180 minutes)
How do I write the line "SinceStatusChange = " so I can later test if the value is greater than 180?
Thanks.
Upvotes: 1
Views: 1112
Reputation: 124642
Well you are trying to assign a double
to a TimeSpan
which, for obvious reasons, will not work. You already have what you want really, just use:
// the 'Minutes' property of a TimeSpan object is a double,
// so the result is a double, not a new TimeSpan object.
double elapsed = (DateTime.Now - StatusDateTime).TotalMinutes;
if( elapsed > 180 )
{
// do stuff
}
Upvotes: 4
Reputation: 310852
You can do this quite simply:
if (DateTime.Now - StatusDateTime > TimeSpan.FromMinutes(180))
{
// ...
}
Upvotes: 2
Reputation: 966
This will give you the double you're looking for:
if (DateTime.Now.Subtract(statusDateTime).TotalMinutes > 180)
{
//do work.
}
Upvotes: 1
Reputation: 498972
You can do this in a more type safe manner, just using TimeSpan
s:
TimeSpan _180mins = new TimeSpan(3,0,0);
var timeDiff = DateTime.Now.Date - StatusDateTime;
if(timeDiff < _180mins)
{
}
Upvotes: 1
Reputation: 5785
The call of TotalMinutes on a TimeSpan returns a double. Change the signature of SinceStatusChange to double and you can use it like
double SinceStatusChange;
SinceStatusChange = (DateTime.Now.Date - StatusDateTime).TotalMinutes;
if (SinceStatusChange > 180)
Upvotes: 1