converting string containing date as YYYYMMDD

  • Thread starter Thread starter Amos
  • Start date Start date
A

Amos

How can I convert a string with the date in YYYYMMDD format to MM/DD/YYYY
format?

Thanks,
Amos
 
Something as simple as:

string newd = old.Substring(4, 2) + "/" + old.Substring(6, 2) + "/" +
old.Substring(0, 4);

- Noah Coad -
Microsoft MVP
 
Amos,

You will have to convert the string to a DateTime instance, and then
back. You can call the ParseExact method, passing in your format of
"YYYYMMDD" (you have to check the casing).

Once you have the result of that, you can call the ToString method on
the DateTime instance, passing in your new format.

Hope this helps.
 
Thanks. I just thought that C# had something in String..Format or ToString()
parameter that could do that.

Angel
 
You might have some luck with the DateTime.Parse static method, then use
ToString() with a format.

James Hebben
 
Hey Amos,

Since you have no delimitor in your date string DateTime.Parse( ..) won't
work.

Try this ...

DateTime date = DateTime.ParseExact("20040326", "yyyyMMdd", null);
date.ToString("MM/dd/yyyy");

Regard,

Du
 

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

Back
Top