开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜