在VB.NET中获取日期差异

时间:2013-12-17 13:08:10

标签: vb.net

我希望得到两个日期时间选择器选择的两个日期之间的区别,包括年,月和日。在单独的文本框中的日子。

我试过了:

txtyrs.text = datediff(datetimepicker1,datetimepicker2)

它不起作用。

10 个答案:

答案 0 :(得分:9)

试试这个:

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

如果您想提取年份,请查看 this post

答案 1 :(得分:5)

使用:

datediff(interval, date1, date2);

例如:间隔可以是日,月,年,小时,秒分钟等。它从date2中减去date1。

在Dateformat中输入date1和date2。

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

输出:4

答案 2 :(得分:2)

这样的事情应该有效:

    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

答案 3 :(得分:2)

proc updateTheTime {} {
    global systemTime displayedTime
    set systemTime [clock seconds]
    set displayedTime [clock format $systemTime -format "%T"]
    after 500 updateTheTime
}
updateTheTime

# ...

ttk::label .time -textvariable displayedTime

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

试试这个:

txtyrs.text=datediff(DateInterval.Year,datetimepicker1,datetimepicker2).ToString()

假设datetimepicker1datetimepicker2属于DateTime类型。如果没有,您需要获取各自的DateTime值并改为使用它们。

答案 6 :(得分:1)

我修改了您的代码,以便您可以轻松理解它。

试试这个:

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

我希望就是这样。如果出现问题或者仍然令人困惑,请告诉我。

答案 7 :(得分:1)

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

 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()

或者,可以检索TimeSpan结构,作为从另一个日期中减去一个日期的结果,然后查询该结构的各个成员。

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属性返回Doubles,而DateDiff总是返回Longs(Int64)。

DateDiff的

175天

4222小时

253345分钟

15200730秒

时间跨度

175.934383644387天

4222.42520746528小时

253345.512447917分钟

15200730.746875秒

答案 8 :(得分:0)

必须进行一些计算,然后

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)

...更多

答案 9 :(得分:0)

我使用可选的EndDate创建了此功能,默认情况下为当前功能。我已经将零功能添加到了时间。

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