Question

Assume that I only have a country code (en, de, fr) and I need to display the weekdays in this language. I know about RegionInfo and CultureInfo - but I can't find a solution. If I create a country info from (for an example) "en" I have no DateTime info in it. It would also be OK to just take the first matching Region. For an example en-US for en or de-DE for de.

I don't know if there are differences in DayNames but I know there are some for the months. de-DE Februar - de-AT Feber -- anyhow I don't care. Event if it may be "a bit different" (to see Februar instead of Feber) - it is still German.

And that's what I want to achive - get en an write Monday - get de and write Montag...

Is there a way to create a region just from a language code?

Était-ce utile?

La solution

This piece may be helpful: to go from "en" to the CultureInfo, a quicker way is

CultureInfo ci = CultureInfo.CreateSpecificCulture("en")

For the second part, I believe you are asking for day names, so you would write

string[] names = ci.DateTimeFormat.DayNames 

Autres conseils

It's usually when you say "I don't care" that you start fighting the APIs.

Anyway, this should work:

var list = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var ci = list.FirstOrDefault(c => c.IetfLanguageTag.StartsWith("de"));

var ri = new RegionInfo(ci.Name);
Console.WriteLine("Today = {0:dddd MMMM yyyy}", DateTime.Today);

This will give you a list of cultures:

    Dim ci As CultureInfo
    For Each ci In CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        Console.WriteLine(ci.Name)
    Next

OUTPUT: ar-SA bg-BG ca-ES zh-TW cs-CZ da-DK de-DE el-GR en-US fi-FI fr-FR he-IL hu-HU is-IS it-IT ja-JP ko-KR nl-NL nb-NO pl-PL pt-BR ro-RO ru-RU hr-HR sk-SK sq-AL sv-SE th-TH tr-TR ur-PK id-ID uk-UA be-BY sl-SI et-EE lv-LV lt-LT fa-IR vi-VN hy-AM az-Latn-AZ eu-ES mk-MK af-ZA ka-GE fo-FO hi-IN ms-MY kk-KZ ky-KG sw-KE uz-Latn-UZ tt-RU pa-IN gu-IN ta-IN te-IN kn-IN mr-IN sa-IN mn-MN gl-ES kok-IN syr-SY dv-MV ar-IQ zh-CN de-CH en-GB es-MX fr-BE it-CH nl-BE nn-NO pt-PT sr-Latn-CS sv-FI az-Cyrl-AZ ms-BN uz-Cyrl-UZ ar-EG zh-HK de-AT en-AU es-ES fr-CA sr-Cyrl-CS ar-LY zh-SG de-LU en-CA es-GT fr-CH ar-DZ zh-MO de-LI en-NZ es-CR fr-LU ar-MA en-IE es-PA fr-MC ar-TN en-ZA es-DO ar-OM en-JM es-VE ar-YE en-029 es-CO ar-SY en-BZ es-PE ar-JO en-TT es-AR ar-LB en-ZW es-EC ar-KW en-PH es-CL ar-AE es-UY ar-BH es-PY ar-QA es-BO es-SV es-HN es-NI es-PR sma-NO bn-BD bs-Cyrl-BA tg-Cyrl-TJ en-SG en-MY mn-Mong-CN prs-AF wo-SN rw-RW qut-GT sah-RU gsw-FR co-FR rm-CH mi-NZ ug-CN br-FR moh-CA arn-CL ga-IE ii-CN sma-SE sr-Latn-BA quz-PE ig-NG kl-GL lb-LU ba-RU nso-ZA quz-BO yo-NG sms-FI ha-Latn-NG hr-BA fil-PH ps-AF fy-NL ne-NP am-ET iu-Cans-CA quz-EC si-LK smj-SE lo-LA km-KH cy-GB bo-CN se-SE smj-NO as-IN ml-IN se-FI or-IN sr-Cyrl-BA bn-IN en-IN dsb-DE tk-TM smn-FI oc-FR es-US se-NO mt-MT bs-Latn-BA zu-ZA xh-ZA tn-ZA tzm-Latn-DZ iu-Latn-CA hsb-DE

DateTime.Today.ToString("dddd", new CultureInfo("zh-TW"))

Above code will print Monday as 星期一 (Chinise locale).

Dim days As String() Dim months As String() Dim cul As New System.Globalization.CultureInfo("de-DE") months = cul.DateTimeFormat.MonthNames days = cul.DateTimeFormat.DayNames

Try this:-

string culture = "your culture"

CultureInfo myCulture = new CultureInfo(culture);

DateTimeFormatInfo dtfi = myCulture .DateTimeFormat;

dayString = dtfi.GetDayName( your date here.. );

Charlie G

As "de" is a NeutralCulture it won't be possible to get the DayNames out of the DateTimeFormat if you try CultureInfo.GetCultureInfo("de").

I would suggest that you run the CultureInfo.CreateSpecificCulture("de") in this case since it'll create a non-neutral culture...in case of "de" I got a culture of "de-DE" back from the .NET gods :D

    CultureInfo tmp = CultureInfo.CreateSpecificCulture("de");

    string[] names = tmp.DateTimeFormat.DayNames;
    foreach(string name in names)
    {
        Console.WriteLine(name);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top