SilverShotBee SilverShotBee - 4 months ago 19
Vb.net 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)))


Where
CaseOpenDays
is a label and
DateReported.text
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

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")
Comments