How to convert a string containing a date to a different format?
Consider this开发者_如何学C string
11/12/2010
I need to convert this to 20101112
.
How can this be done?
Given that the first is a valid date, and the second is a different representation of the date, the easiest method with the least amount of code for your example is:
string newString = DateTime.ParseExact("11/12/2010", "MM/dd/yyyy").ToString("yyyyMMdd")
Try this:
string input = "11/12/2010";
DateTime date = DateTime.Parse(input);
string formattedOutput = date.ToString("yyyyMMdd");
The above as a one-liner:
string formattedOutput = DateTime.Parse("11/12/2010").ToString("yyyyMMdd");
精彩评论