Vb.net的约会功能 [英] Vb.net datediff function

查看:66
本文介绍了Vb.net的约会功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有人可以帮助我在两个日期之间获得准确的dateDiff吗?

我需要知道两个日期之间有多少年约会。







Dim firstDate As Date

Dim SecondDate As Date

firstDate =2016-09-28

SecondDate =现在







TextBox1.Text = DateDiff(DateInterval.Year,firstDate,SecondDate)

'文本框值= 1 ..它应该为零,因为2016年9月28日到今天的日期不等于1整年。



i只能将它除以365或366.


$ b $另一个例子是。

Datediff(dateInterval.year,2015-02-01,2017-01-01)= 2.

它应该只有1。除非当前日期是2017-02-01。



请帮忙。



我的尝试:



Hi All,

Can someone help me getting accurate dateDiff between two dates?
I need to get how many years between two dates.



Dim firstDate As Date
Dim SecondDate As Date
firstDate = "2016-09-28"
SecondDate = Now



TextBox1.Text = DateDiff(DateInterval.Year, firstDate, SecondDate)
'The textbox value = 1 .. it should be Zero because between September 28 2016 to Date today is not equivalent to 1 whole year.

i just cant divide it by 365 or 366.

another example is.
Datediff(dateInterval.year, 2015-02-01 , 2017-01-01 ) = 2 .
it should be 1 only. unless the present date is 2017-02-01.

Please help.

What I have tried:

Dim firstDate As Date
Dim SecondDate As Date
        firstDate = "2016-09-28"
        SecondDate = Now


       
        TextBox1.Text = DateDiff(DateInterval.Year, firstDate, SecondDate)

推荐答案

检查出来:
Imports System
Public Module Module1
    Public Sub Main()
		
		Dim firstdate As New Date(2015, 2, 1, 0, 0, 0)
		Dim seconddate As New Date(2017, 1, 1, 0, 0, 0)
		
		Dim timespan As TimeSpan = seconddate - firstdate
		Dim interval As DateTime = DateTime.MinValue.AddDays(timespan.Days)

		Console.WriteLine("{0} - {1} = {2} years", seconddate, firstdate, interval.Year  - 1)
    End Sub
End Module

参考: TimeSpan结构(系统) [ ^ ]


谢谢Peter。



这很有用。

我也有自己想要分享的解决方案。这给出了准确的结果。



Thank you Peter.

That is helpful.
I also have may own solution i want to share. This gives accurate result.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim x As Integer
        Dim dt1 As Date
        Dim dt2 As Date

        dt1 = DateTimePicker1.Text
        dt2 = DateTimePicker2.Text
        dt1 = dt1.AddYears(x)
        x = DateDiff(DateInterval.Year, dt1, dt2)
        dt1 = dt1.AddYears(x)

        If dt1 <= dt2 Then
            x = x
            TextBox1.Text = x
        Else
            x = x - 1
            TextBox1.Text = x
        End If
    End Sub


End Class


这篇关于Vb.net的约会功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆