我希望找到两个连接到两个单独文本框的ajax日历扩展器的天数差异。
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."
我之前使用asp日历控件完成了此任务,但在尝试使用ajax日历扩展实现此目的时遇到了问题。
标签输出表明许多测试日期“两个日期相隔0天”。
如果有人能提供任何指导/帮助或让我知道我哪里出错,我将不胜感激 谢谢!
答案 0 :(得分:0)
使用TimeSpan对象:)
Hacky示例:
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
将返回:
如果您想要真正的极性(在确定第一个日期是否低于第二个日期时有用),您可以将alwaysPositive arg设置为False,例如: