-
February 3rd, 2006, 02:44 AM
#1
DateDiff function - VB.NET
HI am working with vb.net vs SQL Server 2000.
I have been struggling to get DateDiff function working but still no success.The result of the difference between these two dates ie DATEHIRED and RETIREDATE which should be in months,had always been zero.below is my code;
[CODE]
Public Function DateDiff( _
ByVal Interval As DateInterval, _
ByVal Date1 As Date, _
ByVal Date2 As Date) As Double
End Function[CODE]
[CODE]
Private Sub DateTimePicker3_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker3.Leave
txtElapsed.Text = DateDiff(DateInterval.Month, DateTimePicker3.Value, txtRetireDate.Text)
End Sub[CODE]
NOTEateTimePicker3 holds DATEHIRED value.
number of months is an integer value.
Is there anything wrong with my code?????
Any suggestion will be highly appreciated!!!!!!!
Last edited by kenone; February 3rd, 2006 at 05:50 AM.
-
February 3rd, 2006, 03:22 AM
#2
Re: DateDiff function - VB.NET
-
February 3rd, 2006, 04:13 AM
#3
Re: DateDiff function - VB.NET
hi
hanness
thanks for the link tip.....but the instructions given are too general ..and my program could not work out even after implemented them...am still having the same problem as reported earlier....
-
February 3rd, 2006, 05:18 PM
#4
Re: DateDiff function - VB.NET
Check out your other posts on this forum
http://www.vbforums.com/showthread.php?t=385300
The date and datetime classes have the methods you need...
-
February 4th, 2006, 05:47 AM
#5
Re: DateDiff function - VB.NET
Originally Posted by gigemboy
Thanks for the advice.BUT I think Timespan and Datetime doesnt have ability to calculate number of months(sorry for my little vb.net knowledge).I have tried to define it other way around but didnt work out either.see below;
[VBCODE]Private Sub DateTimePicker3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
Dim MyRetireDate As Date
Dim DateDifference As TimeSpan = DateTimePicker3.Value.Subtract(MyRetireDate)
txtService.Text = DateDifference.TotalDays / 30
End Sub[/VBCODE]
I have divided by 30 to get number of months(may be?)But the result is a strange number(big number)..is there anything else to consider?....sorry once again for your time and effort
-
February 4th, 2006, 01:16 PM
#6
Re: DateDiff function - VB.NET
Ahhh.. once again, I will post here
I must apologize, kenone! DateDiff IS what you need, and it is pretty powerful. I thought this was an old VB6 thing, but it is actually included in the Microsoft.VisualBasic namespace, and not the compatibility namespace (so its ok to use!!!! as it is not "old vb6 ways") Here is an example on how to get the total months....
Code:
D1 = Date.Now
D2 = D1.AddYears(60)
MessageBox.Show(Microsoft.VisualBasic.DateDiff(DateInterval.Month, D1, D2).ToString) 'displays "720"!!!!
You can also use Year, Quarter, all kinds of things...
Kenone, feel free to say "I Told ya so!" and smack me around a few times
-
February 6th, 2006, 02:09 AM
#7
Re: DateDiff function - VB.NET
Thanks very much gig ...However I still had some problems with the datediff function ..in which case the result was always zero as previous.I suspect there is still a convertion problem that I have not noticed up to now though I have tried to use ctype(date) and cdate(date) without a success.
[CODE]
dim months as integer
months = DateDiff(DateInterval.Month, Ctype(txtHireDate.Text), Ctype(txtRetireDate.Text))
txtmonths.text=months
[CODE]
Is there anything with my code above???
Nevertheless,I have used datetime objects....and it seem to work fine.see below;
Dim months As Integer = (endDate.Year - startDate.Year) * 12 + (endYear.Month - startYear.Month)
I might still need to know what was the problem with datediff function...at your convinience.
thanks
-
February 6th, 2006, 07:18 AM
#8
Re: DateDiff function - VB.NET
Originally Posted by kenone
Thanks very much gig ...However I still had some problems with the datediff function ..in which case the result was always zero as previous.I suspect there is still a convertion problem that I have not noticed up to now though I have tried to use ctype(date) and cdate(date) without a success.
Code:
dim months as integer
months = DateDiff(DateInterval.Month, Ctype(txtHireDate.Text), Ctype(txtRetireDate.Text))
txtmonths.text=months
Well you arent using CType correctly. You are not specifying the object to cast it to. Just use the Date.Parse method in order to parse a text string into a date... as in...
Code:
Dim MyDate as Date = Date.Parse(txtHireDate.Text)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|