how to uppercase date and month first letter of ToLongDateString() result in es-mx Culture?
Asked Answered
P

5

7

currently i obtain the below result from the following C# line of code when in es-MX Culture

   Thread.CurrentThread.CurrentCulture =
     Thread.CurrentThread.CurrentUICulture = new
                CultureInfo("es-mx");

  <span><%=DateTime.Now.ToLongDateString()%></span>

miércoles, 22 de octubre de 2008

i would like to obtain the following

Miércoles, 22 de Octubre de 2008

do i need to Build my own culture?

Phonate answered 23/10, 2008 at 4:35 Comment(0)
A
10

You don't need to build your own culture. You only need to change the property DateTimeFormat.DayNames and DateTimeFormat.MonthNames in the current culture.

i.e.

        string[] newNames = { "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo" };
        Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames = newNames;

However, it's weird that en-US show months and days with the first uppercase letter and for mx-ES not.

Hope it helps!.

Auroraauroral answered 23/10, 2008 at 5:0 Comment(4)
"it's weird that en-US show months and days with the first uppercase letter and for mx-ES not" - not really, each follows the grammar rules of its language. In Spanish, day and month names use lower case.Imam
Hey that's true. I'm Mexican I didn't know that the correct way was using lower case. In Mexico many people write months and days using title case, probably because of an US influenceAuroraauroral
if using Microsoft's translation is ok for you you can use Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");Casemaker
The problem with using es-ES is that number formatting is different between Mexico and Spain. You'll get commas instead of periods for the decimal, and decimals where you expect commas.Mccabe
P
3

a little late but this work for me!

 public static string GetFecha()
    {
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("es-EC");
        System.Threading.Thread.CurrentThread.CurrentCulture = culture;

        // maldita sea!
        string strDate = culture.TextInfo.ToTitleCase(DateTime.Now.ToLongDateString());

        return strDate.Replace("De", "de");


    }
Passementerie answered 30/3, 2010 at 3:3 Comment(1)
Worst function name ever. Spanglish is Evil.Tanya
T
1

The pattern of LongDate for Spanish (Mexico) is

dddd, dd' de 'MMMM' de 'yyyy

according to Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern. I guess you just have to manually convert the initial letters of the day and month to uppercase or you can use Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase and then replace "De" with "de".

Tiffany answered 23/10, 2008 at 4:50 Comment(2)
This is not a good approach when your I18Ning your work as it requires intimate knowledge of translations.Aara
yeah my bad. But how would you approach in culture ignostic way?Tiffany
G
1

The custom long date format string for the invariant culture is "dddd, dd MMMM yyyy", so you can use string.Format and a method to handle uppercase:

private string GetDateFormated(DateTime date)
{
    return string.Format("{0}, {1} {2} {3}",
        ToTitleCase(date.ToString("dddd")),
        date.ToString("dd"),
        ToTitleCase(date.ToString("MMMM")),
        date.ToString("yyyy"));
}

private string ToTitleCase(string input)
{
    return input[0].ToString().ToUpper() + input.Substring(1);
}

Reference: The long date ("D") format specifier

Gretna answered 7/1, 2022 at 20:20 Comment(0)
P
0

first two Solutions works fine but what if we would like to extend this to any culture so i came up with this approach i change the current culture date time arrays into TitleCase

private void SetDateTimeFormatNames()
        {

            Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames = ConvertoToTitleCase(Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames);
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames = ConvertoToTitleCase(Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames);

        }

private string[] ConvertoToTitleCase(string[] arrayToConvert)
            {
                for (int i = 0; i < arrayToConvert.Length; i++)
                {
                    arrayToConvert[i] = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(arrayToConvert[i]);
                }

                return arrayToConvert;
            }

how can this be improved with out the Loop?

Phonate answered 23/10, 2008 at 5:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.