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

前端 未结 4 668
栀梦
栀梦 2021-01-05 05:45

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

   Thread.CurrentThread.CurrentCulture =
     Thread.CurrentThr         


        
相关标签:
4条回答
  • 2021-01-05 06:05

    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?

    0 讨论(0)
  • 2021-01-05 06:13

    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");
    
    
        }
    
    0 讨论(0)
  • 2021-01-05 06:19

    The pattern of LongDate for Spanish (Mexico) is

    ffffdd, 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".

    0 讨论(0)
  • 2021-01-05 06:20

    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!.

    0 讨论(0)
提交回复
热议问题