Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 计算并显示两个日期之间的范围_Vb.net_Date_Visual Studio 2008_Date Comparison - Fatal编程技术网

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

Vb.net 计算并显示两个日期之间的范围,vb.net,date,visual-studio-2008,date-comparison,Vb.net,Date,Visual Studio 2008,Date Comparison,我需要示例代码: 今天的日期和到期日期 情况: 如果许可证在2010年2月21日到期,它将计算并显示从到期日(2010年2月21日)到今天(2014年2月21日)的范围 注:产量是月份而不是年份 像这样: 今日日期:2014年2月21日 有效期:2010年2月21日 产出:48个月 提前谢谢 更新 仅月份差异: ''' <summary> ''' Shows the month difference between two dates with custom string forma

我需要示例代码: 今天的日期和到期日期

情况: 如果许可证在2010年2月21日到期,它将计算并显示从到期日(2010年2月21日)到今天(2014年2月21日)的范围

注:产量是月份而不是年份

像这样: 今日日期:2014年2月21日 有效期:2010年2月21日 产出:48个月

提前谢谢

更新

仅月份差异:

''' <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
原创的

我相信您可以自定义此代码段以满足您的需要

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

用法示例:

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)"
这是:


此代码以月为单位返回差值:请注意,这是绝对值。要获取非绝对值,请删除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

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

看看您应该如何提问主题问题。据我所知,您希望以月为单位,而不是以年、月和天为单位。正确吗?@MDTech.us_MAN-是的,我只需要几个月。这个答案并不完全正确。问这个问题的人明确要求时间只以月为单位。见我的答案。更直接、更干净。尽管如此,我还是会删除我的否决票。Clean并不意味着代码行数减少,这并不准确,因为所有月份都没有30天。添加了一个方法,该方法可以解释任何类型的月份。
    Dim date1 As New Date(2010, 2, 21)
    Dim date2 As Date = Date.Today

    Dim differenceInMonths As Integer = MonthDifference(date2, date1)
    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