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

2 Answers 2

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.

10
  • 4
    @Dmirtry: Didn't know that we have such things in .NET, nice But it raises the question why they stopped with the genitive Feb 9, 2017 at 9:23
  • 9
    @Tim Schmelter: Even Russian has 6 standard and 5 obsolete cases; Finnish uses 15, Hungarian 21... Genetive at least is in typical use Feb 9, 2017 at 9:26
  • 2
    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)!
    – Kolichikov
    Feb 9, 2017 at 9:34
  • @TimSchmelter: I would think the list of cases was modeled on English, where only the genitive case differs from the nominative…
    – dumetrulo
    Feb 9, 2017 at 10:00
  • @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).
    – Joey
    Feb 9, 2017 at 10:03
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.