Tepken Vannkorn
Tepken Vannkorn

Reputation: 9723

Datediff() function not getting the expected result in VB.NET

I am using the following code in my project. I want to find the number of days by given the last date and now.

Dim BorrowDate As Date
Dim i As Integer
BorrowDate = Date.Parse(txtBorrowDate.Text)
i = DateDiff(DateInterval.Day, BorrowDate, DateTime.Now)

for example, when BorrowDate is "01/Jul/2011" then the result is 7 days which it should be 10 to now. Please help

Upvotes: 2

Views: 7301

Answers (1)

dbasnett
dbasnett

Reputation: 11773

Since you are using .Net you might try this

    Dim BorrowDate As Date = Date.Parse(txtBorrowDate.Text)

    Debug.WriteLine(BorrowDate.ToString)
    Debug.WriteLine(DateTime.Now.ToString)

    Dim ts As TimeSpan = DateTime.Now - BorrowDate

    Dim numdays As Integer = CInt(ts.TotalDays)

    Debug.WriteLine(numdays.ToString("n0"))

edit: Init the variables and show the dates.

Upvotes: 4

Related Questions