两个日期之间的周/月差异

时间:2014-09-25 20:29:04

标签: vb.net date

我需要找到两个约会之间的差异。

  • 如果月份差异大于6,则显示月份差异
  • 如果少于6个月,请在几周内显示
  • 最后,总是围绕下一个整数(6.2周---> 7)

我试过这个

    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

它适用于某些日期,但不适用于所有情况。

  • 2014/09/01 - > 2015/03/01给了我26周,应该是6个月

有什么想法吗?在vb.net中是否有内置函数我可以使用它来使它更容易?

由于

1 个答案:

答案 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方法冲突。

我希望这会有所帮助