Are there built-in Month name declensions in C#
Asked Answered
O

2

37

I'm wondering if there's any built-in functionality in .NET for declining dates in languages that support noun declensions, (ie. In Russian the month name is февраль, but if I wanted to say the date or say that something is due by, I'd use the form февраля). I made my own version, which works for this case, but I will need to expand to to other cases, and other languages, which will have their own declensions for dates.

Is this functionality built-in, or available in an external library? Thank you for any help.

I've provided my function for the Russian genitive case, if my explanation wasn't clear.

public static string DeclineMonth(this DateTime time)
{
    var month = time.ToString("MMMM");
    if (month.Last() == 'ь')
        return month.Replace('ь', 'я');
    else
        return month + "a";
}       
Oscitancy answered 9/2, 2017 at 9:6 Comment(0)
G
56

You can obtain months' names in two cases, Nominative:

DateTimeFormatInfo info = CultureInfo.GetCultureInfo("ru-RU").DateTimeFormat;

// February: 2 - 1 (array is zero based) - "Февраль"
Console.Write(info.MonthNames[2 - 1]);

And Genitive:

// February: 2 - 1 (array is zero based) - "февраля"
Console.Write(info.MonthGenitiveNames[2 - 1]);

Other cases (e.g. Dative, Accusative) are not supported and we have to implement them manually.

Garv answered 9/2, 2017 at 9:17 Comment(10)
@Dmirtry: Didn't know that we have such things in .NET, nice But it raises the question why they stopped with the genitiveGenus
@Tim Schmelter: Even Russian has 6 standard and 5 obsolete cases; Finnish uses 15, Hungarian 21... Genetive at least is in typical useGarv
Thank you, works great! And as a bonus, returns the month as lower case (which you showed in your code comments, but I missed it, so I was extra pleased)!Oscitancy
@TimSchmelter: I would think the list of cases was modeled on English, where only the genitive case differs from the nominative…Neubauer
@dumetrulo: I'd rather say that date formats around the world only use nominative or genitive month names. In English the two lists don't differ at all, so it's definitely not based on English (which would be unusual for Microsoft, to model CultureInfo only on English conventions).Luminesce
@Joey: Alas, no… In Russian Tool case and Prepositional case are in common use with dates: "The box marked with Feb 2017 label" (Tool case) "Talking about Feb 2017" (Preposition case)Garv
Ok, my Russian is quite rusty after not needing it for more than a decade. Maybe it still holds true for standalone dates (in short and long form).Luminesce
@DmitryBychenko Yes, Hungarian has a bewildering array of cases but most of the time (including in things like "February" or "the 20th of February" but excluding phrases like "in February of 2017") you can get away with nominative.Panelboard
@DmitryBychenko 5 obsolete cases? I think there are only 2 - vocative and ablative that Russian is missing. All Indo-European started out with 8 cases originally.Sesame
@sashoalm: Vocative, Locative, Ablative, Partitive (Genetive II), Accusative IIGarv
D
0

To get "1 января 2023" instead of "1 январь 2023", and also get "1 мая" instead of "1 май", use next code:

CultureInfo culture = new CultureInfo("ru-RU");
culture.DateTimeFormat.MonthNames = culture.DateTimeFormat.MonthGenitiveNames;
culture.DateTimeFormat.AbbreviatedMonthNames = culture.DateTimeFormat.AbbreviatedMonthGenitiveNames;
string result1 = DateTime.Now.ToString("d MMMM yyyy", culture);
string result2 = DateTime.Now.ToString("d MMM", culture);
Disseminate answered 7/10, 2023 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.