如何将 ToLongDateString() 的日期和月份第一个字母大写以产生 es-mx 文化?

发布于 2024-07-07 08:41:28 字数 391 浏览 6 评论 0原文

中从以下 C# 代码行获得以下结果

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

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

目前,我在 es-MX Culture miércoles, 22 de octubre de 2008

我想获得以下

Miércoles, 22 de Octubre de 2008

我需要构建自己的文化吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幽蝶幻影 2024-07-14 08:41:28

你不需要建立自己的文化。 您只需更改当前区域性中的属性 DateTimeFormat.DayNames 和 DateTimeFormat.MonthNames 即可。

ie

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

但是,奇怪的是 en-US 显示第一个大写字母的月份和日期,而 mx-ES 则没有。

希望能帮助到你!。

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

我ぃ本無心為│何有愛 2024-07-14 08:41:28

有点晚了但这对我有用!

 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");


    }

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");


    }
篱下浅笙歌 2024-07-14 08:41:28

西班牙语(墨西哥)的 LongDate 模式是

dddd,dd' de 'MMMM' de 'yyyy

根据 Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern。 我想您只需手动将日期和月份的首字母转换为大写,或者您可以使用 Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase,然后将“De”替换为“de”。

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

撞了怀 2024-07-14 08:41:28

固定区域性的自定义长日期格式字符串是“dddd, dd MMMM yyyy”,因此您可以使用 string.Format 和处理大写的方法:

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);
}

参考:长日期 ("D") 格式说明符

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

于我来说 2024-07-14 08:41:28

前两个解决方案工作正常,但如果我们想将其扩展到任何文化,那么我想出了这种方法,我将当前文化日期时间数组更改为 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;
            }

如何在不使用循环的情况下改进这一点?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文