user961437
user961437

Reputation:

Get days as an int from a timespan?

Currently I have this:

var x = Convert.ToString(dateTimePicker2.Value.Subtract(dateTimePicker1.Value));
int xDays = Convert.ToInt32(x.Substring(0,1));

But it's ugly and makes me unhappy on the inside (Also doesn't work for 2 digit day spans, e.g. 15). Is there a better way to do this?

Upvotes: 9

Views: 23977

Answers (1)

Anthony Pegram
Anthony Pegram

Reputation: 126992

TimeSpan.Days returns an integer value representing the days component of the timespan value.

 var span = someDate.Subtract(anotherDate);
 int days = span.Days;

Upvotes: 30

Related Questions