开发者

How to convert a string containing a date to a different format?

开发者 https://www.devze.com 2023-01-23 19:23 出处:网络
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 da

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消