How to format DateTime in C #?

3

I know two ways to format a date in C# :

data.ToShortDateString which gives me a return like this: 07/21/2017

data.ToLongDateString that gives me a return like this: Friday, July 21, 2017

I want a format similar to ToLongDateString , but without the day of the week, eg: July 21, 2017.

How to do it?

    
asked by anonymous 21.07.2017 / 21:08

1 answer

8

ToString accepts format.

Notice that it is necessary to escape the word "from" because d would be considered as day and that date formats are sensitive to the application culture, so it may be necessary to specify the culture in the method.

var dataFormatada = DateTime.Now.ToString("dd \"de\" MMMM \"de\" yyyy");
var data = DateTime.Now.ToString("dd \"de\" MMMM \"de\" yyyy", new CultureInfo("pt-BR"));

See an example in .NET Fiddle.

    
21.07.2017 / 21:17