Results 1 to 26 of 26

Thread: Date Handling

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Date Handling

    I need to calculate (days till due) based on a date tyoed into a text box.

    also

    I need to calculate (age) based on birth date entered into a text box.

    I have a form made--
    txtFutureDate
    btnCalcDueDays
    txtBirthDate
    btnCalcAge

    all on one form.....

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Date Handling

    VB Code:
    1. MsgBox DateDiff("d", Now, CDate(txtDate.Text))
    Tells how many full days are between the two dates.

    Similarly, you can get age simply by giving "y" as the parameter to DateDiff instead of "d".

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    I am using .net not basic

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date Handling

    I presume you mean VB.Net, so I have moved the thread here.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    I want years not days on birthdate.

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

    Re: Date Handling

    Well Timespan doesn't have years, just days, I believe DateDiff has years, has previously stated, but DateDiff is also a Runtime Function carried over from VB6. You can use Date.Subtract to subtract today from the birthday, getting a timespan, then you can divide the totaldays by 365, and truncate the result (to get rid of the remainder) in order to get current age, like the example below:
    VB Code:
    1. Dim MyBirthday As Date = Date.Parse("01/01/1980")
    2.         Dim MyAge As TimeSpan = Date.Now.Subtract(MyBirthday)
    3.         '365.25 is used to help accound for leap years
    4.         MessageBox.Show(Math.Truncate(MyAge.TotalDays / 365.25).ToString)

    Of course, this is not particularly exact, because of the leap years, so you may want to go with DateDiff...

    **EDIT - well, i'm not too sure DateDiff can do it either, you can divide by 365.25 in order to get a more exact age...
    Last edited by gigemboy; Aug 5th, 2006 at 03:39 PM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    Does not work right. I need to refresh message box. when I put in a new date the message does not change.

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

    Re: Date Handling

    The messagebox was just to show you the result as an example, and the code was just to show you an example of using Date.Subtract. This isn't meant to be the exact code you would use in your program. You can store what is inside the messagebox call into a string in order to keep the age.. or remove the .tostring conversion and store it in a numeric variable... but the code works and works with any date you input (assuming its a valid date that the Date object can handle)...
    Last edited by gigemboy; Aug 5th, 2006 at 04:23 PM.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    Okay ..

    now how do I import the current date to my code.
    (message box)
    example:


    current date: 08/05/2002
    Future date: 08/10/2002
    Days till due: 5
    [OK]

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

    Re: Date Handling

    The original post is subtracting the birthdate from the current date...

    Date.Now ...

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    I don't need time:

    day of week, month day, year(4)
    friday, december 30, 2006

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    Private Sub btnCalcAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAge.Click
    MessageBox.Show( _
    "Current Date: " & Date.Today & _
    ControlChars.CrLf & _
    ControlChars.CrLf & _
    "Birth date: " & (txtBirthDate.Text) & _
    ControlChars.CrLf & _
    ControlChars.CrLf & _
    "Age: " & _
    Math.Round(-DateDiff("y", Today, CDate(txtBirthDate.Text)) / 365.25, 2), "Age Calculation")

    txtBirthDate.Text = ""
    txtBirthDate.Focus()

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

    Re: Date Handling

    Date.Now is a date object... it would be handy if you look at the methods of the date class if you are not sure... you will notice that there are methods to return all of what you had asked...
    VB Code:
    1. MessageBox.Show(Date.Now.ToLongDateString)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    MessageBox.Show( _
    "Current Date: " & Date.Now.ToLongDateString & _
    ControlChars.CrLf & _
    ControlChars.CrLf & _
    "Birth date: " & Date.Now.ToLongDateString(txtBirthDate.Text) & _
    ControlChars.CrLf & _
    ControlChars.CrLf & _
    "Age: " & _
    Math.Round(-DateDiff("y", Today, CDate(txtBirthDate.Text)) / 365.25, 2), "Age Calculation")

    /////////
    blue line under birthdate......

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

    Re: Date Handling

    Well you aren't using the methods right... nowhere did I pass in text to a parameter of the .ToLongDateString method, you added that yourself, and it doesn't work that way. Did you not look at my post at all? I used Date.Parse in order to parse text into a date object...

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    are you using vb.net because what you gave me was a debug error.

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

    Re: Date Handling

    look at post #3 that you typed... and look at where this thread is... it is in the VB.NET forum...
    Quote Originally Posted by cats
    I am using .net not basic
    Its why it was moved here...

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    I will try again, although I need the format that was given in the past reply.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    I need to calculate (days till due) based on a date tyoed into a text box.

    also

    I need to calculate (age) based on birth date entered into a text box.

    I have a form made--
    txtFutureDate
    btnCalcDueDays
    txtBirthDate
    btnCalcAge

    all on one form.....

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

    Re: Date Handling

    I don't know how else to put it as it has already been discussed in my earlier posts...

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

    Re: Date Handling

    So 19 posts later you've simply restated your original question. Are you saying that you have got nothing from all the posts that have been made?

    First of all, getting date input in a TextBox is a bad idea. You should be using a DateTimePicker control, which handles date formatting and validation automatically. If you have to use a TextBox then the only reason would be because this is homework, in which case it's not appropriate for us to just give you the code.

    If you do use a DateTimePicker then you can get the entered date value from its Value property. If you use a TextBox then you should validate the data because the user could enter anything at all:
    VB Code:
    1. Dim input As Date
    2.  
    3. If Date.TryParse(myTextBox.Text, input) Then
    4.     'The text was a vaid date, ehich is now contained in the "input" varaible.
    5. Else
    6.     'The text was not a vaid date.
    7. End If
    That takes care of getting the date that the user enters. You can get today's date from the Date.Today property. Once you have two Date objects you get the number of days between them by subtracting one from the other to get a TimeSpan object and then using its Days property. You can get the number of years between the two Dates by either using the DateDiff function or else simply by subtracting their Year properties and then comparing their DayOfYear properties to see if the current year is complete or not.

    As I said, if this is not homework then you should use a DateTimePicker. If you start using one then I can offer more help if it's needed. If this is homework and you must use a TextBox then I'd have to see a start from you to offer more assistance, or else a more specific question that indicates that you're making an effort yourself.
    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

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    I told you this below does not work:
    /////////////////

    Dim MyBirthday As Date = Date.Parse("01/01/1980")
    Dim MyAge As TimeSpan = Date.Now.Subtract(MyBirthday)
    '365.25 is used to help accound for leap years
    MessageBox.Show(Math.Truncate(MyAge.TotalDays / 365.25).ToString)

    /////////////////

    Example 1 of 2
    future date: 10/04/2006
    Current Date: 08/05/2006
    days till due: 60

    Example 2 of 2
    Birth date: Monday, June 16, 1980
    Current date: Saturday, August 5, 2006
    age: 26


    Private Sub btnCalcAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAge.Click
    MessageBox.Show( _
    "Current Date: " & Date.Now.ToLongDateString & _
    ControlChars.CrLf & _
    ControlChars.CrLf & _
    "Birth date: " & Date.Parse(txtBirthDate.Text) & _
    ControlChars.CrLf & _
    ControlChars.CrLf & _
    "Age: " & _
    Math.Round(-DateDiff("y", Today, CDate(txtBirthDate.Text)) / 365.25, 2), "Age Calculation")

    txtBirthDate.Text = ""
    txtBirthDate.Focus()

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

    Re: Date Handling

    It works on Example 2 of 2, which is what I was responding to... the original (1 of 2) is a different question, that just needs to use the .Subtract method of the future date, passing the Current date to the method and getting the total days using the .Days property of the Timespan result...
    Last edited by gigemboy; Aug 5th, 2006 at 07:56 PM.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Location
    Wichita
    Posts
    19

    Re: Date Handling

    can you show me.

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

    Re: Date Handling

    I have already shown you. I am retiring from this thread.

    P.S. I loved the "Not Helping" rep you gave me too...
    Last edited by gigemboy; Aug 5th, 2006 at 08:02 PM.

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

    Re: Date Handling

    cats, we're not here to do your homework for you. If you're going to rep people down because you don't understand the help they're trying to provide you aren't going to make many friends. When someone asks a question regarding their homework the usual response is to provide guidance and then allow you to work out the details. You should not just take literally what someone says or posts and throw up your hands when it doesn't work. Read carefully what has been posted. Try to get an understanding of the principles of what it's trying to do.

    Did you read what I posted? It's pretty basic stuff in principle. You get two Date objects. You subtract the earlier from the later to get a TimeSpan that represents the time between the two. It has a Days property that tells you the number of whole days between the two dates. DateDiff is also pretty straightforward. Have you read the documentation about the function so you can UNDERSTAND what it does, rather than just assuming that it will do what you want? I'm guessing not becuase otherwise you'd have used different parameters. According to the documentation your first parameter should be "yyy" or DateInterval.Year. I don't know what that fourth parameter is trying to achieve because the argument is supposed to be a FirstDayOfWeek value.

    Also, if you want just the current date you should be using Date.Today. Date.Now is the current date AND time.
    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

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