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

Consider this line

11/12/2010

I need to convert this to 20101112.

How can I do that?

+3
source share
2 answers

Given that the first is a valid date, and the second is a different representation of the date, the simplest method with the least code for your example:

string newString = DateTime.ParseExact("11/12/2010", "MM/dd/yyyy").ToString("yyyyMMdd")
+6
source

Try the following:

string input = "11/12/2010";
DateTime date = DateTime.Parse(input);
string formattedOutput = date.ToString("yyyyMMdd");

Above as single line:

string formattedOutput = DateTime.Parse("11/12/2010").ToString("yyyyMMdd");
+1
source

Source: https://habr.com/ru/post/1773127/


All Articles