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.....
Printable View
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.....
Tells how many full days are between the two dates.VB Code:
MsgBox DateDiff("d", Now, CDate(txtDate.Text))
Similarly, you can get age simply by giving "y" as the parameter to DateDiff instead of "d".
I am using .net not basic
I presume you mean VB.Net, so I have moved the thread here.
I want years not days on birthdate.
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...
Does not work right. I need to refresh message box. when I put in a new date the message does not change.
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)...
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]
The original post is subtracting the birthdate from the current date...
Date.Now ...
I don't need time:
day of week, month day, year(4)
friday, december 30, 2006
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()
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)
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......
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...
are you using vb.net because what you gave me was a debug error.
look at post #3 that you typed... and look at where this thread is... it is in the VB.NET forum...
Its why it was moved here...Quote:
Originally Posted by cats
I will try again, although I need the format that was given in the past reply.
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.....
I don't know how else to put it as it has already been discussed in my earlier posts...
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: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.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
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.
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()
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...
can you show me.
I have already shown you. I am retiring from this thread.
P.S. I loved the "Not Helping" rep you gave me too...
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.