Adding "de" to es-ES CultureInfo DateTime C #

Probably a dumb question, but I haven't worked with Culture before. The assets for the date must be:

MMMM dd "de" yyyy (without quotes on "de").

junio 1 de 2015

What I have now:

CultureInfo ci = new CultureInfo("es-ES", false);
string date = DateTime.Now.ToString("MMMM dd REPLACE yyyy", ci).Replace("REPLACE", "de");

      

I want to know if there is a "more programmatically correct" way to do this that I am not aware of, not about string.Replace

+3


source to share


3 answers


Just quote the literal part with single quotes, double quotes, or escape each backslash character:

string date = DateTime.Now.ToString("MMMM dd 'de' yyyy", ci);
string date = DateTime.Now.ToString("MMMM dd \"de\" yyyy", ci);
string date = DateTime.Now.ToString(@"MMMM dd \d\e yyyy", ci);

      



Note that in the second option, we need to escape "

inside the string literal, and in the third option, I use the string literal to avoid escaping backslashes. Using single quotes is the easiest :)

Read Custom Date and Time Format Strings for more details on what you can do.

+5


source


If I understand clearly, you can escape the part de

with a single quote like:

string date = DateTime.Now.ToString("MMMM dd 'de' yyyy", ci);

      

or with a double quote;

string date = DateTime.Now.ToString("MMMM dd \"de\" yyyy", ci);

      

or escaped as a verbatim string literal ;



string date = DateTime.Now.ToString(@"MMMM dd \d\e yyyy", ci);

      

I almost prefer to use a single quote, because "

, and \

can be confusing.

Additional Information:

+2


source


You can escape backslash characters:

string date = DateTime.Now.ToString(@"MMMM dd \d\e yyyy", ci)

      

See the documentation.

Note @

that prevents backslashes from being parsed as a string escaped string.

+2


source







All Articles