Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

.net - 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";
}       
question from:https://stackoverflow.com/questions/42132299/are-there-built-in-month-name-declensions-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...