How to format Datetime in C#?

Asked

Viewed 416 times

3

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

data.ToShortDateString that gives me a return like this: 21/07/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, ex: 21 de July de 2017.

How to do?

1 answer

8


The ToString accepted format.

Realize that it is necessary to escape the word "from" because the d would be considered as day and which date formats are sensitive to the application culture, so it may be necessary to specify the crop 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 on . NET Fiddle.

Browser other questions tagged

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