-3

I am trying different cultures in spanish language and testing some latin and south american cultures.

When using "es-MX" culture the console shows me correctly the date, but when using "es-CO" culture the console shows me some unkown characters.

The following code samples shows the result obtained when executing:

    class Program
    {
        static void Main(string[] args)
        {
            //Mexico Culture
            CultureInfo culture = new CultureInfo("es-MX");
            DateTime today = DateTime.Today;

            string dateFormat = today.ToString(culture);
            Console.WriteLine(dateFormat);

            //Result: 02/04/2025 12:00:00 a. m.

            Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Colombia Culture
            CultureInfo culture = new CultureInfo("es-CO");
            DateTime today = DateTime.Today;

            string dateFormat = today.ToString(culture);
            Console.WriteLine(dateFormat);

            //Result: 2/04/2025 12:00:00?a.?m.

            Console.ReadLine();
        }
    }

Can anybody tell me how to change any configuration to prevent that unknown characters?

5
  • Wonder if that is reproducible @mjwills ... dotnetfiddle.net/piNKWm Commented Apr 1 at 23:03
  • @Cleptus It is, but you habe to look at the data and not its lookalike or presentation ;o) Commented Apr 2 at 4:46
  • @mjwills I have just updated the question following your recommendation, thanks Commented Apr 2 at 17:28
  • @SirRufo what do you mean by data? Commented Apr 2 at 17:29
  • @iEligio You will never know if two strings are equal if you compare their presentation f.i. on the console. Have a look at the real data (byte-array) of the strings with Encoding.UTF8.GetBytes. Same bytes, same string Commented Apr 2 at 19:21

1 Answer 1

1

Looks like the issue comes from different Unicode spaces used in certain cultures. Some Spanish-speaking locales (like "es-CO") use a non-breaking space (U+00A0) instead of a regular space (U+0020). Depending on your console, this might show up as weird characters.

Fix:

You can simply replace those non-breaking spaces with normal spaces before displaying the date:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        DateTime now = DateTime.Now;

        // Using "es-MX" (Mexico)
        string formattedMX = now.ToString("F", new CultureInfo("es-MX"));
        Console.WriteLine("es-MX: " + formattedMX);

        // Using "es-CO" (Colombia)
        string formattedCO = now.ToString("F", new CultureInfo("es-CO"));

        // Replace non-breaking spaces with regular spaces
        formattedCO = formattedCO.Replace("\u00A0", " ");
        Console.WriteLine("es-CO: " + formattedCO);
    }
}

Some consoles (especially the Windows Command Prompt) might not support certain characters properly. You can try forcing UTF-8 encoding before printing:

Console.OutputEncoding = System.Text.Encoding.UTF8;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.