|
-
Aug 5th, 2006, 11:54 AM
#1
Thread Starter
Junior Member
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.....
-
Aug 5th, 2006, 12:52 PM
#2
Re: Date Handling
VB Code:
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".
-
Aug 5th, 2006, 01:34 PM
#3
Thread Starter
Junior Member
Re: Date Handling
I am using .net not basic
-
Aug 5th, 2006, 02:09 PM
#4
Re: Date Handling
I presume you mean VB.Net, so I have moved the thread here.
-
Aug 5th, 2006, 03:02 PM
#5
Thread Starter
Junior Member
Re: Date Handling
I want years not days on birthdate.
-
Aug 5th, 2006, 03:32 PM
#6
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:
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)
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.
-
Aug 5th, 2006, 04:16 PM
#7
Thread Starter
Junior Member
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.
-
Aug 5th, 2006, 04:19 PM
#8
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.
-
Aug 5th, 2006, 04:48 PM
#9
Thread Starter
Junior Member
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]
-
Aug 5th, 2006, 04:50 PM
#10
Re: Date Handling
The original post is subtracting the birthdate from the current date...
Date.Now ...
-
Aug 5th, 2006, 05:26 PM
#11
Thread Starter
Junior Member
Re: Date Handling
I don't need time:
day of week, month day, year(4)
friday, december 30, 2006
-
Aug 5th, 2006, 05:28 PM
#12
Thread Starter
Junior Member
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()
-
Aug 5th, 2006, 05:29 PM
#13
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:
MessageBox.Show(Date.Now.ToLongDateString)
-
Aug 5th, 2006, 05:33 PM
#14
Thread Starter
Junior Member
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......
-
Aug 5th, 2006, 05:36 PM
#15
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...
-
Aug 5th, 2006, 05:38 PM
#16
Thread Starter
Junior Member
Re: Date Handling
are you using vb.net because what you gave me was a debug error.
-
Aug 5th, 2006, 05:39 PM
#17
Re: Date Handling
look at post #3 that you typed... and look at where this thread is... it is in the VB.NET forum...
 Originally Posted by cats
I am using .net not basic
Its why it was moved here...
-
Aug 5th, 2006, 07:07 PM
#18
Thread Starter
Junior Member
Re: Date Handling
I will try again, although I need the format that was given in the past reply.
-
Aug 5th, 2006, 07:08 PM
#19
Thread Starter
Junior Member
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.....
-
Aug 5th, 2006, 07:40 PM
#20
Re: Date Handling
I don't know how else to put it as it has already been discussed in my earlier posts...
-
Aug 5th, 2006, 07:43 PM
#21
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:
Dim input As Date
If Date.TryParse(myTextBox.Text, input) Then
'The text was a vaid date, ehich is now contained in the "input" varaible.
Else
'The text was not a vaid date.
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.
-
Aug 5th, 2006, 07:47 PM
#22
Thread Starter
Junior Member
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()
-
Aug 5th, 2006, 07:53 PM
#23
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.
-
Aug 5th, 2006, 07:55 PM
#24
Thread Starter
Junior Member
-
Aug 5th, 2006, 07:57 PM
#25
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.
-
Aug 5th, 2006, 08:21 PM
#26
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.
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
|