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!
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:
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.