datetime.tostring month and day language

半城伤御伤魂 提交于 2019-11-28 23:24:12

Those patterns describe year, month, day and other parameter locations in output result of Datetime. But month and day names are taken from CultureInfo object, not pattern. There's DateTime.ToString overload that supports passing CultureInfo parameter along with format

        CultureInfo culture = new CultureInfo(ISO); 
        DateTime.Now.ToString(culture.DateTimeFormat.LongDatePattern, culture);

This way, ToString() will respect both pattern and names from specified culture

You have only specified the format pattern, so it takes the other settings from the default format. You should also specify the format provider:

myStringDate = myDate.ToString(ci.DateTimeFormat.LongDatePattern, ci);

or using the standard format string D for long date pattern (as it will take the actual pattern from the format provider that you specify):

myStringDate = myDate.ToString("D", ci);

Demo:

CultureInfo ci = new CultureInfo("it-IT");
Console.WriteLine(DateTime.Now.ToString("D", ci));
ci = new CultureInfo("sv-SE");
Console.WriteLine(DateTime.Now.ToString("D", ci));

Output:

giovedì 26 gennaio 2012
den 26 januari 2012

Use DateTimeFormatInfo class.

string code = "mk-MK";
DateTimeFormatInfo info =
    DateTimeFormatInfo.GetInstance(CultureInfo.GetCultureInfo(code));
string longDate = 
    DateTime.Now.ToString(info.LongDatePattern, new System.Globalization.CultureInfo(code));

I just had a similar problem and solved it with:

DateTime.Now.ToString("dddd, dd MMMM yyyy", new System.Globalization.CultureInfo("it-IT"));

That was thanks to this link.

DateTime.ToString() has yet another overload with which you can specify both the pattern you want (as you are currently and the culture to use. Try:

myStringDate = myDate.ToString(ci.DateTimeFormat.LongDatePattern,
                               ci);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!