SilverShotBee SilverShotBee - 8 months ago 53 Question

VB.Net Days Between Dates

I'm trying to calculate the days that have passed between today and a date in the past using the following code:

CaseOpenDays.Text = (DateTime.Now.Subtract(CDate(DateReported.Text)))

is a label and
is also a label.

I'm having this error thrown:

Value of type 'System.TimeSpan' cannot be converted to 'String'

What am I missing here?


You have to use TimeSpan.Days or TimeSpan.TotalDays:

Dim timeSinceReporting As TimeSpan = DateTime.Now - CDate(DateReported.Text)
CaseOpenDays.Text = CInt(timeSinceReporting.TotalDays).ToString()

Another way is TimeSpan.ToString:

CaseOpenDays.Text = timeSinceReporting.ToString("dd")