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";
}       
Metoniem
  • 239
  • 2
  • 15
Kolichikov
  • 2,944
  • 31
  • 46

1 Answers1

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 4
    @Dmirtry: Didn't know that we have such things in .NET, nice But it raises the question why they stopped with the genitive – Tim Schmelter Feb 09 '17 at 09: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 – Dmitry Bychenko Feb 09 '17 at 09: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 09 '17 at 09:34
  • @TimSchmelter: I would think the list of cases was modeled on English, where only the genitive case differs from the nominative… – dumetrulo Feb 09 '17 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 09 '17 at 10:03
  • @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) – Dmitry Bychenko Feb 09 '17 at 10:08
  • 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). – Joey Feb 09 '17 at 10:12
  • @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. – biziclop Feb 09 '17 at 15:43
  • @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. – sashoalm Feb 10 '17 at 08:18
  • @sashoalm: Vocative, Locative, Ablative, Partitive (Genetive II), Accusative II – Dmitry Bychenko Feb 10 '17 at 08:22