How to format date that is a string value??

G

Guest

Hi,
I have date in a string format like string strDate = "110204".
Now i want to convert this string to MM/dd/yyyy format. How do i do this????
I used string.Format(strDate, "MM/dd/yyyy") but no effect. I guess i have to
convert this to date and than to string(MM/dd/yyyy). Please help.
Thanks in advance.
faktujaa
 
J

Jay B. Harlow [MVP - Outlook]

faktujaa,
What does 110204 represent?

February 4th of 2011?
April 2nd of 2011?
November 2nd of 2004?
February 11th of 2004?

Something else?


In either case I would use DateTime.ParseExact to convert the string to a
DateTime, then use DateTime.ToString to convert it to the specific format.

Something like:

Const format As String = "MMddyy"
Dim input As String = "110204"
Dim value As DateTime = DateTime.ParseExact(input, format, Nothing)
Dim output As String = value.ToString("MM/dd/yyyy")

You can rearrange the format constant to the proper order the input string
is in.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top