在 VB.NET 中获取日期差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20635037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:20:57  来源:igfitidea点击:

Get date difference in VB.NET

vb.net

提问by ghost21blade

I want to get the difference between two dates chosen by two date time pickers in years, months & days in separate text boxes.

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

I've tried:

我试过了:

txtyrs.text = datediff(datetimepicker1,datetimepicker2)

It is not working.

它不工作。

采纳答案by ghost21blade

Some calculations must be done and then,

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

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)

... More

... 更多的

回答by Bj?rn-Roger Kringsj?

Try this:

尝试这个:

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

And if you want to extract the years, take a look at this post.

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

回答by Manik G

Use:

用:

datediff(interval, date1, date2);

For example: interval can be day, month, year, hours, second minutes, etc. It subtracts date1 from date2.

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

Enter date1 and date2 in Dateformat.

在 Dateformat 中输入 date1 和 date2。

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

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

Output: 4

输出:4

回答by Sikandar Amla

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

回答by tinstaafl

Something like this should work:

这样的事情应该工作:

    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

回答by Mohammad Naim Dahee

Using DateDiff, you call it with different date interval parameters to retrieve the appropriate value:

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

Alternatively, a TimeSpan structure can be retrieved as the result of subtracting one date from another, and then querying the various members of that structure.

或者,可以通过从另一个日期中减去一个日期然后查询该结构的各个成员来检索 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()

The output of the two different methods is nearly identical, except that the TimeSpan properties are returning Doubles, while DateDiff always returns Longs (Int64).

两种不同方法的输出几乎相同,除了 TimeSpan 属性返回 Doubles,而 DateDiff 始终返回 Longs (Int64)。

DateDiff

日期差异

175 Days

175天

4222 Hours

4222 小时

253345 Minutes

253345分钟

15200730 Seconds

15200730 秒

TimeSpan

时间跨度

175.934383644387 Days

175.934383644387 天

4222.42520746528 Hours

4222.42520746528 小时

253345.512447917 Minutes

253345.512447917 分钟

15200730.746875 Seconds

15200730.746875 秒

回答by Muj

I've modified your code so that you can easily understand it.

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

Try this:

尝试这个:

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

I hope this is it. If something wrong or still confusing just inform me.

我希望就是这样。如果有什么问题或仍然令人困惑,请告诉我。

回答by Tim Schmelter

Use TimeSpanand some date calculaton, this should work:

使用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

回答by Douglas Barbin

Try this:

尝试这个:

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

Assuming that datetimepicker1and datetimepicker2are of type DateTime. If not, you need to get their respective DateTimevalues and use those instead.

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

回答by Alexandru-Codrin Panaite

I've created this function with optional EndDate with default the current one. I've added the zero function to the time.

我用可选的 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