Results 1 to 11 of 11

Thread: Please help urgently!!!!!!datediff in vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    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:
    1. Public Overloads Function DateDiff( _
    2.         ByVal Interval As DateInterval, _
    3.         ByVal Date1 As Date, _
    4.         ByVal Date2 As Date) As Integer


    VB Code:
    1. Private Sub DateTimePicker3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
    2.         Dim service As integer
    3.         service = DateDiff(DateInterval.Month, DateTimePicker3.Value,  CDate(txtRetireDate.Text))
    4.         txtService.Text = service.ToString
    5.     End Sub
    I am suspecting that the final statement (txtService.Text = service.ToString)might be the problem???
    noteatetimepicker3 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

  2. #2
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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.

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Please help urgently!!!!!!datediff in vb.net


  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    Re: Please help urgently!!!!!!datediff in vb.net

    Quote 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:
    1. Private Sub DateTimePicker3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
    2.  
    3. Dim MyRetireDate As Date
    4. Dim DateDifference As TimeSpan = DateTimePicker3.Value.Subtract(MyRetireDate)
    5.         txtService.Text = DateDifference.TotalDays / 30
    6. 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

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  7. #7
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    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:
    1. 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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    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
  •  



Click Here to Expand Forum to Full Width