在VB.NET中获取日期差异

Get date difference in VB.NET

我想在单独的文本框中获得两个日期时间选择者在年、月和日中选择的两个日期之间的差异。

我试过了:

1
txtyrs.text = datediff(datetimepicker1,datetimepicker2)

它不工作。


试试这个:

1
2
3
4
5
6
Dim date1 As Date = Date.Now
Dim date2 As Date = date1.AddDays(4.0#)

Dim span = date2 - date1

Dim days As Double = span.TotalDays '=4

如果你想提取这些年,看看这篇文章。


用途:

1
datediff(interval, date1, date2);

例如:间隔可以是天、月、年、小时、第二分钟等。它从日期2中减去日期1。

以日期格式输入日期1和日期2。

格式:DateDiff(DateInterval.Day, Now.Date, Now.AddDays(4).Date)

产量:4


1
2
3
4
5
Dim D1 as Date = Now
Dim D2 as Date = D1.AddDays(10)
Dim difference As
TimeSpan = D2.Subtract(D1)
msgBox("{0} Days", difference.TotalDays) 'THIS WILL RETURN Total No. of Days

这样的方法应该有效:

1
2
3
4
5
6
7
    Dim dateOne = DateTimePicker1.Value
    Dim dateTwo = DateTimePicker2.Value
    Dim diff As TimeSpan = dateTwo.Subtract(dateOne)
    Dim years As Double = diff.TotalDays / 365
    txtyrs.Text = years.ToString
    txtmonsh.Text = (years * 12).ToString
    txtdays.Text = diff.TotalDays.ToString

使用datediff,可以使用不同的日期间隔参数调用它来检索适当的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 Dim D1, D2 As Date
    D1 = Date.Now
    D2 = #11/9/2004#
    'DateDiff
    Console.WriteLine("DateDiff")
    Console.WriteLine()
    Console.WriteLine("{0} Days", _
        DateDiff(DateInterval.Day, D1, D2))
    Console.WriteLine("{0} Hours", _
        DateDiff(DateInterval.Hour, D1, D2))
    Console.WriteLine("{0} Minutes", _
        DateDiff(DateInterval.Minute, D1, D2))
    Console.WriteLine("{0} Seconds", _
        DateDiff(DateInterval.Second, D1, D2))
    Console.WriteLine()

或者,可以通过从另一个日期中减去一个日期,然后查询该结构的各个成员来检索时间跨度结构。

1
2
3
4
5
6
7
8
Console.WriteLine("TimeSpan")
    Console.WriteLine()
    Dim difference As TimeSpan = D2.Subtract(D1)
    Console.WriteLine("{0} Days", difference.TotalDays)
    Console.WriteLine("{0} Hours", difference.TotalHours)
    Console.WriteLine("{0} Minutes", difference.TotalMinutes)
    Console.WriteLine("{0} Seconds", difference.TotalSeconds)
    Console.WriteLine()

这两个不同方法的输出几乎相同,只是TimeSpan属性返回的是double,而datediff总是返回long(int64)。

DATEDIFF

175天

4222小时

253345分钟

15200730秒

时限

175.934383644387天

4222.42520746528小时

253345.512447917分钟

15200730.746875秒


我已经修改了您的代码,以便您可以很容易地理解它。

试试这个:

1
txtyrs.text = DateDiff(DateInterval.Day, datetimepicker1.value,datetimepicker2.value)

我希望就是这样。如果有什么问题或者仍然很困惑,请通知我。


试试这个:

2

假设datetimepicker1datetimepicker2DateTime型。如果没有,则需要获取它们各自的DateTime值,并使用这些值。


使用TimeSpan和一些日期计算,这应该有效:

1
2
3
4
5
6
7
8
9
10
Dim offset = New Date(1, 1, 1)
Dim dateOne = DateTimePicker1.Value
Dim dateTwo = DateTimePicker2.Value
Dim diff As TimeSpan = dateTwo - dateOne
Dim years = (offset + diff).Year - 1
Dim months = (dateTwo.Month - dateOne.Month) + 12 * (dateTwo.Year - dateOne.Year)
Dim days = diff.Days
TxtYear.Text = years.ToString
TxtMonth.Text = months.ToString
TxtDays.Text = days.ToString

我用可选的结束日期创建了这个函数,默认为当前的结束日期。我在时间上加了零函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Public Shared Function Zero(ByVal Number As Integer) As String
    If Number < 10 Then
        Return"0" & Number.ToString
    Else
        Return Number.ToString
    End If
End Function

Public Shared Function TimeDifference(ByVal StartDate As DateTime, Optional ByVal EndDate As DateTime = Nothing) As String
    If EndDate = Nothing Then
        EndDate = Date.Now()
    End If
    Dim timediff As TimeSpan
    If EndDate > StartDate Then
        timediff = EndDate - StartDate
        Return timediff.Days &":" & Zero(timediff.Hours) &":" & Zero(timediff.Minutes) &":" & Zero(timediff.Seconds)
    Else
        timediff = StartDate - EndDate
        Return timediff.Days &":" & Zero(timediff.Hours) &":" & Zero(timediff.Minutes) &":" & Zero(timediff.Seconds)
    End If
End Function


必须进行一些计算,然后,

1
2
3
dim ts as TimeSpan = dtpicker1.value - dtpicker.value
dim days, months, years as integer
months = 12*(dtp1.value.year - dtp2.value.year) (dtp1.value.month-dtp2.value.month)

…更多