SilverShotBee SilverShotBee - 1 year ago 98 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?

Answer Source

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")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download