I have many strings like “20120117” and “20120321”. I need to convert it in a new string with this format: “2012/01/17” and “2012/03/21”. So, there is a way to do this?
I try:
string dateString = string.format("{0:d", "20120321");
and
string dateString = string.format("{0:yyyy/MM/dd", "20120321");
and
string dateString = int.Parse("20120321").ToString("yyyy/MM/dd");
I all cases i don’t reach my goal. =/
So, i can i do this?
OBS: There is a way to do that without parse to datetime?
You have to parse those values in
DateTimeobjects first.Example :
Edit after your comments on other answers:
if you don’t like parsing because it may throw excepations, you can always use
TryParse, like this:Edit 2: Using
TryParseExactwith multiple formats:It will produce
2012/03/21when using “20120321” as input value, and2012/01/01when using2012as input value.