Results 1 to 11 of 11

Thread: Date Calculations - Vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    Date Calculations - Vb.net

    i Have a program in which VB.NET frontEnd and SQL Server2000 at the backend ,that should calculate dates as follows:
    RETIREDATE = BIRTHDATE + 60YEARS. BirthDate should be input manually while RETIREDATE should be automatically displayed as a result of adding 60years to BIRTHDATE.The code doesnt work at all as it just keep display a strange RETIREDATE ie 01/01/0001,below is my code ;

    VB Code:
    1. Public Overloads Function DateAdd( _
    2.            ByVal Interval As DateInterval, _
    3.            ByVal Number As Integer, _
    4.            ByVal DateValue As DateTime _
    5.         ) As DateTime
    6.        
    7.     End Function


    VB Code:
    1. Private Sub txtDOB_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDOB.Leave
    2.         Dim rdate As DateTime
    3.         Dim dob As DateTime
    4.  
    5.         rdate = DateAdd(DateInterval.Year, 60, txtDOB.Text)
    6.         txtRetireDate.Text = rdate.ToShortDateString
    7.     End Sub
    note: DOB stands for DATEOFBIRTH
    txtRetireDate should be automatically displayed after txtDOB.text being lost focus (on leave event).
    Is there any thing wrong with my code bove??please help

  2. #2
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199

    Re: Date Calculations - Vb.net

    How bout using: txtRetireDate.Text = CType(txtDOB.Text, Date).AddYears(60)

    Here you convert the text in the textbox to a date before adding to it, using the inherent date manipulation functions. Also, it's a one liner.

    I would lean toward a date control as opposed to a textbox as it only allows correct dates. If you leave it up to a textbox, you are going to need to validate the date or else it will throw an exception and crash.

    nJoy
    If the facts don't fit the theory, change the facts. --Albert Einstein

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

    Re: Date Calculations - Vb.net

    The code looks good. Is there any reason your overloading the DatAdd though? When you overload something like that, you're saying "do this instead of what you would normally do." Since you don't have any code in there (whether you left it out intentionally or not); it's not performing the same way as it used to.

    If you need to overload it for whatever reason, but still want the same functionality, you can add:
    VB Code:
    1. Return Microsoft.VisualBasic.DateAdd(Interval, Number, DateValue)
    To your overloaded function. Otherwise, if you're not using it for anything; you can just remove it and let the Fx do what it intended to.

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

    Re: Date Calculations - Vb.net

    Just use the .AddYears method of the Date Class...
    Code:
            Dim MyBirthDay As Date = Date.Parse("01/07/1980")
            Dim MyRetireDay As Date = MyBirthDay.AddYears(60)
            MessageBox.Show(MyRetireDay.ToString)

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

    Re: Date Calculations - Vb.net

    Use a DateTimePicker for the date of birth and a ReadOnly TextBox for the retirement date. You would then just use this code in the DTP's Leave event handler:
    Code:
    myTextBox.Text = myDateTimePicker.Value.AddYears(60).ToShortDateString()
    You would probably be better using the ValueChanged event of the DTP though, as unlike a TextBox it will always have a valid date in it.
    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    Re: Date Calculations - Vb.net

    thanks very much guys...your suggestions worked fine ie make use of DTP and Ctype in my code.
    But still I have one more problem with a DateDiff function as follows;I just want to display number of months elapsed between two years ie HIREDATE and RETIREDATE..I have been struggling hard to use DateDIFF function but upto now am getting a zero(0 )as number of months elapsed.Below is my code;
    VB Code:
    1. Public Function DateDiff( _
    2.         ByVal Interval As DateInterval, _
    3.         ByVal Date1 As Date, _
    4.         ByVal Date2 As Date) As Double
    5.     End Function

    VB Code:
    1. Private Sub DateTimePicker3_Leave(ByVal sender As Object, ByVal e As    System.EventArgs) Handles DateTimePicker3.Leave
    2.  
    3. txtElapsed.Text = DateDiff(DateInterval.Month, DateTimePicker3.Value, txtRetireDate.Text)
    4.  
    5. End SUb
    note: txtElapsed is an integer value in the SQL Server database.
    DateTimePicker3.Value handles HIREDATE.
    Any suggestion will be highly appreciated!!!
    tHANKS

  7. #7
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199

    Re: Date Calculations - Vb.net

    Again, your datatype conversions (or lack of them) are breaking your code...

    try txtElapsed.Text = DateDiff(DateInterval.Month, DateTimePicker3.Value, CType(txtRetireDate.Text, Date))

    Remember... you always want to convert the values inside a textbox that are not supposed to be strings to the correct datatype
    If the facts don't fit the theory, change the facts. --Albert Einstein

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

    Re: Date Calculations - Vb.net

    Don't use DateDiff... there are methods of the date and datetime class that can do this.. like the .Subtract method of the date class... look at your other thread for my example

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    95

    Re: Date Calculations - Vb.net

    Quote Originally Posted by Rockhopper
    Again, your datatype conversions (or lack of them) are breaking your code...

    try txtElapsed.Text = DateDiff(DateInterval.Month, DateTimePicker3.Value, CType(txtRetireDate.Text, Date))

    Remember... you always want to convert the values inside a textbox that are not supposed to be strings to the correct datatype
    thanks for the tip BUT your suggestion didnt worked out...it still give out a zero value as the difference...belo is the code;
    txtElapsed.text = DateDiff(DateInterval.Month, DateTimePicker3.Value, CType(txtRetireDate.Text, Date))

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

    Re: Date Calculations - Vb.net

    I don't know how many times I can say this.. but the Date class has ALL of these methods you are wanting to find. You can display total days, total months, total years, etc of the difference, and its VERY easy, if you use the Date Class... There is NO need for DateDiff, but if you wish to still use it, it is up to you...

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

    Re: Date Calculations - Vb.net

    I must apologize Kenone I did in your other thread. DateDiff IS what you need, and an example is in your other thread here :

    http://www.vbforums.com/showthread.php?p=2342389

    It is in the Microsoft.VisualBasic namespace, and not the compatibility namespace, so it is ok to use!! without people saying it is "old vb6 ways" hehe...

    Anyway.. smack me around a few times for steering you in the wrong direction, and look at the other thread for a very simple example of getting total months...
    Last edited by gigemboy; Feb 4th, 2006 at 01:12 PM.

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