我需要找到两个约会之间的差异。
我试过这个
Dim startDate As Date = txtStartDate.DateValue
Dim endDate As Date = txtEndDate.DateValue
Dim dateDiff As TimeSpan = endDate - startDate
Dim totalDays As Double = dateDiff.TotalDays
'Divide by 7 to get the number of weeks
Dim weeks As Double = totalDays / 7
'6 months, average of 4.34812 weeks per month
If (weeks >= 26.08872) Then
txtDuration.Text = Math.Ceiling((weeks / 4.34812)).ToString() + " Months"
Else
txtDuration.Text = Math.Ceiling(weeks).ToString() + " Weeks"
End If
它适用于某些日期,但不适用于所有情况。
有什么想法吗?在vb.net中是否有内置函数我可以使用它来使它更容易?
由于
答案 0 :(得分:2)
马克,
试试这个。
diff = dateDiff(DateInterval.Month, startDate, endDate)
If (diff > 6) Then
TxtDuration.Text = diff
Else
TxtDuration.Text = dateDiff(DateInterval.WeekOfYear, startDate, endDate)
End If
您必须重命名TimeSpan变量dateDiff,因为它会与DateDiff方法冲突。
我希望这会有所帮助