[Resolved] Help with converting date!
I am trying create code that prompts a user for the date in "mm/dd/yy" form then will display in a messagebox as "Month day, year".
Example: 05/11/06 - will display - May 11, 2006
I tryed using select case for it, but that didn't work to well for me, and is probably longer then another way out there. Could somebody please help?
Thanks.
Re: Help with converting date!
VB Code:
Dim dateString As String = "05/11/06"
Dim myDate As Date = Date.ParseExact(dateString, "dd/MM/yy", Nothing)
MessageBox.Show(myDate.ToString("MMM dd, yyyy"))
Re: Help with converting date!
If the user is prompted for a date using a DateTimePicker control then it is really easy.
The reuslt of the DateTimePicker will be it DateTimePicker.Value - which is a Date type,and as succh can be redisplayed using a ToString
ie, dtpInputDate.Value.Tostring("mmm dd, yyyy")
If you are using a textbox or similar to enter the date (and why - the datetimepicker is so much more user friendly) then your best bet is to convert the textstring to a date type.
I am pretty sure that
VB Code:
dim objDate as Date
objDate = ctype(txtInput.Text, Date)
Will not work in format dd/mm/yy - and besides if the user makes a mistake typing it in, the cast will cause an exception to occur.
Is there any reason you dont want to use a datetimepicker?
Re: Help with converting date!
or you can just do what jmcilhinny suggests :blush:
Re: Help with converting date!
Is this allowing input of any date? It seems this is just getting the date from the computer. I was trying to input any date in "mm/dd/yy" form into a textbox, then it would convert it to the "Month day, year" form displaying it in a messagebox.
I probably should have used more examples in my first post. Sorry @_@
Examples:
06/18/02 - will display - June 18, 2002
05/11/06 - will display - May 11, 2006
Re: Help with converting date!
substitute your txtbox.text for jmc's datestring
VB Code:
Dim myDate As Date = Date.ParseExact(txtbox.Text, "dd/MM/yy", Nothing)
MessageBox.Show(myDate.ToString("MMM dd, yyyy"))
Re: Help with converting date!
Ok, thanks alot everyone :D!
Re: [Resolved] Help with converting date!
You may want to stick a try/catch block around it though otherwise it will all go titsup if the user types in garbage.