0

I am looking to find the difference in days of two ajax calendar extenders that are linked to two separate text boxes.

Dim dt1 As DateTime = Convert.ToDateTime(CalendarExtender1.SelectedDate)
Dim dt2 As DateTime = Convert.ToDateTime(CalendarExtender2.SelectedDate)
Dim diffInDays As Integer = dt2.Subtract(dt1).Days

Label12.text = "The dates are " + diffInDays.ToString() + "days appart."

I have completed this task using the asp calendar control previously but I am having issues when trying to achieve this using the ajax calendar extension.

the label output is stating " the two dates are 0 days apart " for numerous tested dates.

I would appreciate if anyone could give any guidance/help or let me know where i am going wrong.
Thanks!

Anuga
  • 2,619
  • 1
  • 18
  • 27
C. McG
  • 9

1 Answers1

0

Use the TimeSpan object :)

Hacky example:

Sub Main()
    Console.WriteLine(getDayDifference(CDate("2017-06-03"), CDate("2017-06-03"), True).ToString)
    Console.WriteLine(getDayDifference(CDate("2017-06-03"), CDate("2017-05-03"), True).ToString)
    Console.WriteLine(getDayDifference(CDate("2017-02-03"), CDate("2017-05-03"), True).ToString)

    Console.ReadKey()
End Sub

Public Function getDayDifference(first As DateTime, second As DateTime, alwaysPositive As Boolean)
    Dim span As TimeSpan = second - first

    Return If(alwaysPositive, Math.Abs(span.TotalDays), span.TotalDays)
End Function

Will return:

  • 0
  • 31
  • 89

You can set the alwaysPositive arg to False if you want the true polarity (helpful when determining if the first date is lower than the second), eg:

  • 0
  • -31
  • 89
dbl4k
  • 71
  • 6