Converting a string to DateTime for formatting?

I have the following string: 2008-10-17

How would I go about formatting this into : 17/10/2008

I was thinking the easiest solution would be to create a DateTime object and set the date to : 2008-10-17. Then format the date element.

But I am not certain how I would set the date element of the DateTime. Obviously I would be open to far better options?
directxBOBAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ladarlingCommented:
The datetime object has built in return overloads. Try this:

Dim someDate As String = "2008-10-17"
Dim realDate As DateTime
realDate = Date.Parse(someDate)
MsgBox(realDate.ToShortDateString)
 
0
directxBOBAuthor Commented:
Cheers that works but not in the case where dates would be in the format of:

17/10/2008
or
17/10/08

Any ideas on how to get that formatted correctly?
0
ladarlingCommented:
Those two formats would be really problematic unless they were standard throughout the application and there was not the possibility of getting well-formed dates confused with them. What I mean is:
4/5/2008, in this scenario, could mean April 5th, 2008, or it could mean May 4th, 2008. Not good.
But, if you know that you are always going to recieve dates is that format, you can fix them with something similar to:
 

 Public Function FixDate(ByVal aDate As String) As DateTime
        Dim theDay, theMonth, theYear As String
        theDay = Mid(aDate, 1, 2)
        theMonth = Mid(aDate, 4, 2)
        theYear = Mid(aDate, 7) 'Take the rest of the digits
        Return DateTime.Parse(theMonth & "/" & theDay & "/" & theYear)
 
 End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.