converting string from a NumericUpDown output to Date

matchlock

New member
Joined
Dec 30, 2010
Messages
3
Programming Experience
Beginner
hi. i'm a newbie here, and an amateur in programming. i was wondering if you could help me answer my problem with the program that i'm writing. i'm using 3 NumericUpDown controls to get an output and convert it to a Date. i don't know if i am doing this right, and i've been working on this overnight. i'll put the codes that i've written:

Private Sub btnDaysOld_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDaysOld.Click
Dim shoMonth As Short
Dim shoDay As Short
Dim shoYear As Short
Dim strBirthday As String
Dim datBirthday As Date
Dim lngDaysOld As Long

shoMonth = Convert.ToString(nudBirthMonth.Value)
shoDay = Convert.ToString(nudBirthDay.Value)
shoYear = Convert.ToString(nudBirthYear.Value)

strBirthday = shoMonth.ToString & shoDay.ToString & shoYear.ToString

datBirthday = Date.Parse(strBirthday).ToShortDateString
lngDaysOld = DateDiff(DateInterval.Day, datBirthday, Today)
lblDaysOld.Text = lngDaysOld.ToString

End Sub

i'm trying to get the interval days between the two Dates (the Date today, and my birthday). the bolded texts are the ones that i want to know if i'm doing the right thing. because when i run the program, the error says "Format Exception was unhandled: String was not recognized as a valid DateTime".

how can i combine the output from those nud controls to convert it to a string, and then to a Date? i would greatly appreciate your help to solve my problem. Thanks..
 
The whole point of a NumericUpDown is to get a number. Why convert those numbers to text if what you really want is a date, which you can create from the numbers? DO NOT convert anything to a String unless you specifically need a String. Dates are made up of numbers, not text.
VB.NET:
Dim birthday As New Date(CInt(nudBirthYear), CInt(nudBirthMonth), CInt(nudBirthDay))
Dim daysOld As Integer = (Date.Today - birthday).Days
Subtracting a Date from a Date produces a TimeSpan, which can be used to calculate time periods up to a number of days. Units greater than days require a reference date, which a TimeSpan has no concept of.
 
i did what you wrote, but the error still comes up. before i do the code, i've read somewhere that you have to collect the output first as a string, then convert it to a Date. i have to do concatenation first, and the format should be like #mm/dd/yyyy#. i don't know if i did something wrong...

thanks for the reply though
 
If you did what I wrote then that error could not possibly have occurred because at no point is a String being converted to a Date in my code. You might get an exception thrown if the values don;t represent an actual date, like February 30, but the error message would be different. Either you don't get that error message or you aren't doing what I suggested. I think you should show us the exact code you're now using.

Having said all that, why go through all this rigmarole when you could simply use a DateTimePicker, the user could select a date in one go, the control would do all the validation for you and you'd simply get a Date from its Value property?
 
Back
Top