Convert date 20171130 to 30/11/2017 format in string

2

I have this date field '20171130' saved in the database, and I need to convert to string formatting "30/11/2017" in my application, how to do this type of conversion? .

    
asked by anonymous 01.12.2017 / 17:55

2 answers

4

You can use DateTime.TryParseExact to convert to < in> string on an instance of DateTime and then do any formatting.

var strDate = "20171130"; // Este é o valor recuperado do banco
DateTime dateValue;
DateTime.TryParseExact(strDate, "yyyyMMdd", CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, out dateValue);     

Console.WriteLine(dateValue.ToString("dd/MM/yyyy"));

See working on .NET Fiddle.

    
01.12.2017 / 18:00
0

No need to convert. If you are fetching the data from a database, it is already picked up by select. Example:

SELECT *,date_format('data','%d/%m/%Y') as 'data_formatada' FROM tabela

So it takes all the data from the table and the new date will be named data_format.

    
01.12.2017 / 18:05