One of the problems with datediff function is that even if the two dates are 31-12-2011 and 01-01-2012,it will show the difference as 1 year.How to overcome this problem????

SO how do u want to display the date difference??? in months, year or days??? whatever parameter u will give in the date diff function it will be dependent on that....

No, Icannot be, or you did it wrong. Check this example:

``````Dim d1 As New DateTime(2011, 12, 31)
Dim d2 As New DateTime(2012, 1, 1)
Dim ts As TimeSpan = d2.Subtract(d1)
Console.WriteLine("Difference between these two times is:" & vbCr & vbLf & "years: {0}" & vbCr & vbLf & "months: {1}" & vbCr & vbLf & "days: {2}", Math.Round(ts.TotalDays / 366, 1), Math.Round(ts.TotalDays / 31), ts.TotalDays)
``````

I agree you must be entering the wrong syntax...

e.g. I want to find the difference between date1 and date2 in days,
datediff( day, date1, date2)

I want to know the difference in Hours datediff( hour, date1, date2)

if u are entering the datediff as years then it will show 1 yr as 2011 and 2012 has a diff of 1....

I need it in years in a way that it shows 1 only if actually an year has passed ,,otherwise 0(as with above parameters)

then first calculate the number of days and check if actually its greater than 365 days then u can use what u want....

`````` Dim d1 As New DateTime(2010, 12, 31)
Dim d2 As New DateTime(2012, 1, 1)
Dim days As Long
days = DateDiff("d", d1, d2)
Debug.Print(days.ToString & " Days")
If days < 365 Then
MsgBox("Year not completed")
Else
MsgBox("Year completed")
End If
``````

U 're not getting it maybe..I need to get the digit returned and then it will be inserted into column and the two dates are such that one is the current date and other we get from one of the datetimepicker textbox

Yes so instead of displaying the msgbox u can keep a track of the number of years...

Check the scneario what I have done...

2 datetimepicker - 1 with current date and the second that will be derived from datetimepicker2

one label that will give the number of years only if it has completed the year.

``````Private Sub DateTimePicker2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker2.ValueChanged

Dim d1 As DateTime = DateTimePicker1.Value
Dim d2 As DateTime = DateTimePicker2.Value
Dim Year1 As Integer
Dim days As Long
days = DateDiff("d", d2, d1)
Debug.Print(days.ToString & " Days")
If days < 365 Then
Year1 = 0
Else
Year1 = days / 365
End If
Label1.Text = Year1.ToString
End Sub
``````

Hope this is what u need

Can you use DateTime and TimeSpan?
They've never failed me.

I have used datetime...in this timespan wont be required...right??? coz he just wants the days....

When you subtract a DateTime from a DateTime, it creates a TimeSpan.

``````Module Module1
Sub Main()
Dim dtOne As New DateTime(2011, 12, 31) '31 Dec, 2011'
Dim dtTwo As New DateTime(2012, 1, 1) ' 1 Jan, 2012'
Dim tsDiff As TimeSpan = (dtTwo - dtOne)
''
System.Diagnostics.Debug.WriteLine(String.Format("DAYS={0}", tsDiff.Days))
End Sub
End Module
``````

The easiest way to do a calculation like that is to count the days.

Okkk...after getting the days from the timespan we can apply the condition and get what is required...

``````'" & DateDiff("yyyy", DateTimePicker1.Text, Now(), FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFourDays) & "'
``````

ok this is the query..I need the result to be exact.By exact i mean it should only result in 1 when there is actual gap of 1 year not that one of the dates is dec.2011 and other jan. 2012 and i get 1 (which is actually what i am getting).hope i make myself clear

In that case you are misusing the term "exact". Unless you mean you want a result of 1 when the difference is exactly one year. If you are not interested in fractions of a year then do as was suggested and find the difference in days then take the integer portion and throw away the rest. That way you get the numbers

0-364 days = 0 years
365-729 days = 1 year
730-1094 days = 2 years
etc

here you go

‘” & Int(DateDiff("d", DateTimePicker1.Text, Now(), FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFourDays) / 365) & "'

`````` Dim BornDate As Date = DateTimePicker3.Value
Dim Interval As TimeSpan = Now - BornDate
Debug.Print("Interval: " + Interval.TotalDays.ToString)
Dim years As Integer = Math.Truncate(Interval.TotalDays / 365)
TextBox1.Text = years
``````

This part will return exact one year difference if true
'datetimepicker3 is the value that will be changing
it has been compared with Todays date....