怎么算没有。日历中 2 个日期之间的天数,字符串格式 vb
How to count no. of days between 2 dates from calendar in string format vb
我有 2 个 aspx 日历(AbsenceStartDateCal 和 AbsenceEndDateCal),它们会将日期值传递给带有 .cs 代码的文本框,如下所示:
Protected Sub AbsenceEndDateCal_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles AbsenceEndDateCal.SelectionChanged
Dim date2 As DateTime = New System.DateTime
date2 = AbsenceEndDateCal.SelectedDate
tbAbsenceEndDate.Text = date2.ToString("dd/MM/yyyy")
End Sub
在我的页面加载中,我想计算 no.of 天来显示为标签 btw 2 文本框值。我尝试使用以下代码,但它不适用于此错误 "String was not recognized as a valid DateTime."
Dim startDate As DateTime = Convert.ToDateTime(tbAbsenceStartDate.Text)
Dim endDate As DateTime = Convert.ToDateTime(tbAbsenceEndDate.Text)
Dim ts As New TimeSpan
ts = endDate.Subtract(startDate)
lblnoOfDays.Text = ts.Days
所以我不确定,因为我是 VB 的新手。任何帮助将不胜感激。
您可以使用 ParseExact 方法并指定您想要的格式。
您得到无效的格式,因为您可能试图设置超出当前格式下定义的范围的日期或月份值。
示例:
Dim startDateString = "12/01/2015"
Dim endDateString = "14/01/2015"
Dim startDate = DateTime.ParseExact(startDateString, "dd/mm/yyyy", CultureInfo.InvariantCulture)
Dim endDate = DateTime.ParseExact(endDateString, "dd/mm/yyyy", CultureInfo.InvariantCulture)
Dim ts As New TimeSpan
ts = endDate.Subtract(startDate)
Dim dayDiff = ts.Days
我有 2 个 aspx 日历(AbsenceStartDateCal 和 AbsenceEndDateCal),它们会将日期值传递给带有 .cs 代码的文本框,如下所示:
Protected Sub AbsenceEndDateCal_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles AbsenceEndDateCal.SelectionChanged
Dim date2 As DateTime = New System.DateTime
date2 = AbsenceEndDateCal.SelectedDate
tbAbsenceEndDate.Text = date2.ToString("dd/MM/yyyy")
End Sub
在我的页面加载中,我想计算 no.of 天来显示为标签 btw 2 文本框值。我尝试使用以下代码,但它不适用于此错误 "String was not recognized as a valid DateTime."
Dim startDate As DateTime = Convert.ToDateTime(tbAbsenceStartDate.Text)
Dim endDate As DateTime = Convert.ToDateTime(tbAbsenceEndDate.Text)
Dim ts As New TimeSpan
ts = endDate.Subtract(startDate)
lblnoOfDays.Text = ts.Days
所以我不确定,因为我是 VB 的新手。任何帮助将不胜感激。
您可以使用 ParseExact 方法并指定您想要的格式。 您得到无效的格式,因为您可能试图设置超出当前格式下定义的范围的日期或月份值。
示例:
Dim startDateString = "12/01/2015"
Dim endDateString = "14/01/2015"
Dim startDate = DateTime.ParseExact(startDateString, "dd/mm/yyyy", CultureInfo.InvariantCulture)
Dim endDate = DateTime.ParseExact(endDateString, "dd/mm/yyyy", CultureInfo.InvariantCulture)
Dim ts As New TimeSpan
ts = endDate.Subtract(startDate)
Dim dayDiff = ts.Days