The other day I was working on a project where it was important to get the full name of the month from a specified date. So off I went to start writing an array that contained the full name of all the months. In the back of my mind, however, my coder’s conscience was letting me know this solution was a path to problems. I started thinking of the maintenance nightmares that would ensue when issues such as abbreviated month names and multi-lingual support would start to creep in (and turned out to be a requirement for this particular client). The last thing I want to do is try to keep up with lists of months and abbreviations in French or German or Hindi. So I went in search of a better solution and my travels revealed the .NET framework already supported this functionality.
The secret can be found in the CultureInfo class. Not only did this eliminate the need for multiple lists for names and abbreviations, it also has multi-lingual support. Score!
The method looks like this:
1: CultureInfo.CurrentCulture.
2: DateTimeFormat.GetMonthName(1);
And this returns January. I didn’t want to have to type this out every time a month name was needed so it was time for the help of an extension method:
1: public static class DateTimeExtensions
2: {
3: public static string GetMonthName(this DateTime dt)
4: {
5: return GetMonthName(dt,
6: CultureInfo.CurrentCulture);
7: }
8:
9: public static string GetMonthName(this DateTime dt,
10: CultureInfo currentCulture)
11: {
12: return currentCulture.DateTimeFormat
13: .GetMonthName(dt.Month);
14: }
15: }
Now I can get the full name of a month from any culture. But wait, there’s more…
1: public static class DateTimeExtensions
2: {
3: public static string GetMonthName(this DateTime dt)
4: {
5: return GetMonthName(dt,
6: CultureInfo.CurrentCulture);
7: }
8:
9: public static string GetMonthName(this DateTime dt,
10: CultureInfo currentCulture)
11: {
12: return currentCulture.DateTimeFormat
13: .GetMonthName(dt.Month);
14: }
15:
16: public static string GetAbbreviatedMonthName(
17: this DateTime dt)
18: {
19: return GetAbbreviatedMonthName(dt,
20: CultureInfo.CurrentCulture);
21: }
22:
23: public static string GetAbbreviatedMonthName(
24: this DateTime dt,
25: CultureInfo currentCulture)
26: {
27: return currentCulture.DateTimeFormat
28: .GetAbbreviatedMonthName(dt.Month);
29: }
30:
31: public static string GetDayName(this DateTime dt)
32: {
33: return GetDayName(dt,
34: CultureInfo.CurrentCulture);
35: }
36:
37: public static string GetDayName(this DateTime dt,
38: CultureInfo currentCulture)
39: {
40: return currentCulture.DateTimeFormat
41: .GetDayName(dt.DayOfWeek);
42: }
43:
44: public static string GetAbbreviatedDayName(this
45: DateTime dt)
46: {
47: return GetAbbreviatedDayName(dt,
48: CultureInfo.CurrentCulture);
49: }
50:
51: public static string GetAbbreviatedDayName(
52: this DateTime dt,
53: CultureInfo currentCulture)
54: {
55: return currentCulture.DateTimeFormat
56: .GetAbbreviatedDayName(dt.DayOfWeek);
57: }
58: }
1: string date = "1/1/2010";
2: string monthName = DateTime.Parse(date)
3: .GetMonthName();
Extension methods rock - this is an ideal use case.
ReplyDeleteI've used them on DataReaders for getting values, like GetNullableInt(string column) instead of checking for dbnull, casting, blah, blah. I should publish that....