DateDiff function - VB.NET
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: DateDiff function - VB.NET

  1. #1
    Join Date
    Aug 2005
    Posts
    24

    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 04:50 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

  3. #3
    Join Date
    Aug 2005
    Posts
    24

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

  4. #4
    Join Date
    Jan 2006
    Posts
    293

  5. #5
    Join Date
    Aug 2005
    Posts
    24

    Re: DateDiff function - VB.NET

    Quote Originally Posted by gigemboy
    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...
    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

  6. #6
    Join Date
    Jan 2006
    Posts
    293

    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

  7. #7
    Join Date
    Aug 2005
    Posts
    24

    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

  8. #8
    Join Date
    Jan 2006
    Posts
    293

    Re: DateDiff function - VB.NET

    Quote 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center