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

Asked

Viewed 155 times

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 ?.

  • At what point do you want to do this conversion, young man? In the bank or in the application?

  • Good, I’ll edit to specify. @LINQ

  • because Voce did not save in the bank so "30/11/2017"?

  • Why do I get a json in this format and already saved directly in the bank. @Juliohenrique97

2 answers

4


It is possible to use DateTime.TryParseExact to convert the string in 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.

  • Remove the new from Cultureinfo.Invariantculture, because it is static... the way it is not working...

  • @samuelrvg had left the old code. Obg

0

You don’t have to convert. If you are taking the data from a database, it already gets converted 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 with the data_formatted name.

  • Blz, but in this case how would select ? use Entity Framework

  • I think sql is the same in all languages, because you will directly manipulate the database. So just adapt this query to your C code#.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.