Converting a string to DateTime for formatting?

Posted on 2008-11-04
Last Modified: 2012-05-05
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?
Question by:directxBOB
    LVL 11

    Expert Comment

    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)

    Author Comment

    Cheers that works but not in the case where dates would be in the format of:


    Any ideas on how to get that formatted correctly?
    LVL 11

    Accepted Solution

    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


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now