|
-
Feb 3rd, 2006, 09:06 AM
#1
Thread Starter
Lively Member
Please help urgently!!!!!!datediff in vb.net
Am working with vb.net and Sql Server2000.on my program has to calculate the difference between two dates ,the result should be in months.ie RETIREDATE - HIREDDATE=number of months served.Surprisingly ,am getting ZERO months as a result.below is my code;
VB Code:
Public Overloads Function DateDiff( _
ByVal Interval As DateInterval, _
ByVal Date1 As Date, _
ByVal Date2 As Date) As Integer
VB Code:
Private Sub DateTimePicker3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
Dim service As integer
service = DateDiff(DateInterval.Month, DateTimePicker3.Value, CDate(txtRetireDate.Text))
txtService.Text = service.ToString
End Sub
I am suspecting that the final statement (txtService.Text = service.ToString)might be the problem???
note atetimepicker3 holds DATEHIRED.
Please help this urgently.!!!!!.I have been trying to figure it out in several ways to no avail.
I hope I will get a helpful support.thanks
-
Feb 3rd, 2006, 09:14 AM
#2
Fanatic Member
Re: Please help urgently!!!!!!datediff in vb.net
Is there a reason that you are overloading the DateDiff function? To my knowledge, the DateDiff function will give you what you need right out of the box.
Put in a breakpoint and see what the values look like. Make sure that DateTimePicker3.Value is what you want. Make sure txtRetireDate.Text is what you want. Make sure that the service variable is showing the correct value.
Also, switch your variables around in the DateDiff function and see if that helps. To my understanding, the highest date goes first and then the lowest date if you want a positive number.
-
Feb 3rd, 2006, 10:11 AM
#3
Re: Please help urgently!!!!!!datediff in vb.net
-
Feb 3rd, 2006, 01:43 PM
#4
Re: Please help urgently!!!!!!datediff in vb.net
You shouldn't need DateDiff anymroe. There are methods of the Date and DateTime class that already has this functionality, like the .Subtract method of the date class... the code below subtracts today from the retireday, and displays how many days it is until then...
Code:
Dim DateNow As Date = Date.Now
Dim MyRetireDate As Date = Date.Parse("12/01/2006")
Dim DateDifference As TimeSpan = MyRetireDate.Subtract(DateNow)
MessageBox.Show(DateDifference.TotalDays & ":TotalDays, ") 'displays 300 and some change
-
Feb 4th, 2006, 05:34 AM
#5
Thread Starter
Lively Member
Re: Please help urgently!!!!!!datediff in vb.net
 Originally Posted by gigemboy
Dim DateNow As Date = Date.Now
Dim MyRetireDate As Date = Date.Parse("12/01/2006")
Dim DateDifference As TimeSpan = MyRetireDate.Subtract(DateNow)
MessageBox.Show(DateDifference.TotalDays & ":TotalDays, ") 'displays 300 and some change
[/code]
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;
VB Code:
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
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
-
Feb 4th, 2006, 12:26 PM
#6
Re: Please help urgently!!!!!!datediff in vb.net
Ahhh Total Months... hmmm... Well you can get total days of the resulting timespan and find some way to do it.. this worked fine for me:
Code:
Dim MyBirthDay As Date = Date.Parse("01/07/1980")
Dim MyRetireDay As Date = MyBirthDay.AddYears(60)
Dim MyDifference As TimeSpan = MyRetireDay.Subtract(MyBirthDay)
MessageBox.Show(MyDifference.TotalDays.ToString) 'total days
MessageBox.Show((MyDifference.TotalDays / 30).ToString) 'approx days
MessageBox.Show((MyDifference.TotalDays / 30 / 12).ToString) 'approx years
You would essentially have to get the total days in each full year of the difference, and in each month of the beginning and final year.. it can be done, just takes a little more code...
-
Feb 4th, 2006, 12:40 PM
#7
Re: Please help urgently!!!!!!datediff in vb.net
My question would be this, why have you started another thread with exactly the same post when you were already receiving alot of help and support in your first thread which appears to still be very active?
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Feb 4th, 2006, 12:57 PM
#8
Re: Please help urgently!!!!!!datediff in vb.net
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
-
Feb 4th, 2006, 06:52 PM
#9
Re: Please help urgently!!!!!!datediff in vb.net
You are doing something REALLY screwy. The DateDiff Runtime function is a member of a module. To be overloading an existing function you would have to have inherited the class it belongs to, but you can't inherit a module so basically you CANNOT overload the existing DateDiff function. That begs the question why you have posted code that indicates that you are? Get rid of that declaration and just call the existing DateDiff function, as Gig suggested.
You can get a number of months using DateTime objects without too much trouble, although it can't be done quite as easily as it can with DateDiff:
VB Code:
Dim months As Integer = (endDate.Year - startDate.Year) * 12 + (endYear.Month - startYear.Month)
This does not take into account whether the current month is complete or not, which would require additional code.
And I STRONGLY second the motion about not starting duplicate threads, especially when people are already helping you in the first. What does that say to them about their efforts? Certainly nothing complimentary.
-
Feb 6th, 2006, 02:12 AM
#10
Thread Starter
Lively Member
Re: Please help urgently!!!!!!datediff in vb.net
[Code]
You can get a number of months using DateTime objects without too much trouble, although it can't be done quite as easily as it can with DateDiff:
VB Code:
Dim months As Integer = (endDate.Year - startDate.Year) * 12 + (endYear.Month - startYear.Month)
This does not take into account whether the current month is complete or not, which would require additional code.
And I STRONGLY second the motion about not starting duplicate threads, especially when people are already helping you in the first. What does that say to them about their efforts? Certainly nothing complimentary.[/QUOTE]
Thanks for the suggestion,datetime objects worked fine....am still working on year differences issues.just some few code to add.
I am sorry for the duplicate posts from me....I was just under stress and I felt exhausted..very sorry
Thanks for the hint.
-
Feb 6th, 2006, 02:15 AM
#11
Thread Starter
Lively Member
Re: Please help urgently!!!!!!datediff in vb.net
guys sorry for the duplicate posts........i really appreciate your concern.......sorry for any inconvinience caused.......was due to working under pressure and being exhausted..sorry
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
|