计算并显示两个日期之间的范围

时间:2014-02-21 01:49:24

标签: vb.net date visual-studio-2008 date-comparison

我需要示例代码: Date_today和到期日期

情况: 如果许可证于2010年2月21日到期,它将计算并显示date_today(2014年2月21日)至expiration_date(2010年2月21日)之间的范围。

注意:输出是月份而不是年份。

像这样: 今日日期:2014年2月21日 到期日:2010年2月21日 产量:48个月

提前致谢。

2 个答案:

答案 0 :(得分:0)

  

更新

只有月份差异:

''' <summary>
''' Shows the month difference between two dates with custom string format.
''' </summary>
''' <param name="Date1">Indicates the first date to compare.</param>
''' <param name="Date2">Indicates the second date to compare.</param>
Private Function DateDifference(ByVal Date1 As DateTime,
                                ByVal Date2 As DateTime) As Integer

    Dim MonthDiff As Integer

    Do Until Date1 > Date2
        Date1 = Date1.AddMonths(1)
        MonthDiff += 1
    Loop

    Return MonthDiff - 1

End Function


用法:

    Dim MonthDiff As Integer = DateDifference(DateTime.Parse("01/03/2013 00:00:00"),
                                              DateTime.Parse("09/04/2014 01:01:01"))

    MsgBox(String.Format("Month Difference {0}", CStr(MonthDiff)))
  

ORIGINAL

我确信您可以自定义此代码段以满足您的需求。

它返回一个带有差异的自定义字符串格式。

用法示例:

MsgBox(DateDifference(DateTime.Parse("01/03/2013 00:00:00"),
                      DateTime.Parse("09/04/2014 01:01:01"),
                      "{0} Year(s), {1} Month(s), {2} Week(s), {3} Day(s), {4} Hour(s), {5} Minute(s) and {6} Second(s)"

这是:

' Date Difference
' ( By Elektro )
'
''' <summary>
''' Shows the difference between two dates with custom string format.
''' </summary>
''' <param name="Date1">Indicates the first date to compare.</param>
''' <param name="Date2">Indicates the second date to compare.</param>
''' <param name="StringFormat">
''' Indicates the string format to display the difference, where:
''' {0} = Years, {1} = Months, {2} = Weeks, {3} = Days, {4} = Hours, {5} = Minutes and {6} = Seconds</param>
''' <returns>System.String.</returns>
Private Function DateDifference(ByVal Date1 As DateTime,
                                ByVal Date2 As DateTime,
                                ByVal StringFormat As String) As String

    Dim Time As TimeSpan
    Dim YearDiff As Integer, MonthDiff As Integer, WeekDiff As Integer

    Do Until Date1 > Date2

        Date1 = Date1.AddMonths(1)
        MonthDiff += 1

        If MonthDiff = 12 Then
            YearDiff += 1
            MonthDiff = 0
        End If

    Loop

    MonthDiff -= 1
    Date1 = Date1.AddMonths(-1)
    Time = (Date2 - Date1)
    WeekDiff = (Time.Days \ 7)
    Time = (Time - TimeSpan.FromDays(WeekDiff * 7))

    Return String.Format(StringFormat, YearDiff, MonthDiff, WeekDiff, Time.Days, Time.Hours, Time.Minutes, Time.Seconds)

End Function

答案 1 :(得分:0)

此代码返回以月为单位的差异:(请注意,这是绝对值。要获取非绝对值,请删除Math.Abs()函数。)

    Public Shared Function MonthDifference(lValue As DateTime, rValue As DateTime) As Integer
        Return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year))
    End Function

用法:

    Dim date1 As New Date(2010, 2, 21)
    Dim date2 As Date = Date.Today

    Dim differenceInMonths As Integer = MonthDifference(date2, date1)

这是一种更直接的方式,但它没有说明没有30天的月份。

    Dim date1 As New Date(2010, 2, 21)
    Dim date2 As Date = Date.Today

    Dim difference As TimeSpan = date2.Subtract(date1)
    Dim differenceInMonths As Integer = difference.TotalDays / 30

如果您需要使用不同的度量单位(如天,分钟等)来修改答案,请修改第二种方法。