Search code examples
c#.netdatetimedatetime-formatglobalization

Are there built-in Month name declensions in C#


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

Solution

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