.Net:- C# – Get List of Month Names

Background

I found myself developing a small sample code learning LINQ.

I wanted to find data based on dates.

Rather than to display the month as a number, I opted to display as a name.

In essence to be able to hard code the number 3 for the 3rd month, yet “beautifully” display the entry as March.

 

Sample Code

Ample explanation came from:-

How to list all month names, e.g. for a combo?
Link

 

mmmeff


var months1 = Enumerable.Range(1, 12)
                .Select
                 (
                    i => new 
                         { 
                              monthNumber = i
                            , monthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(i)                                         
                         }
                 );
                        

 

Tiago Ávila

tiagoavila.com.br
Alfenas, Brazil
Link


var months2 = Enumerable.Range(1, 12)
               .Select
                (
                   i => new 
                        { 
                            monthNumber = i
                          , monthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(i) 
                        }
                );
                        

 

SeanH


Dictionary<int, string> months3; 
   
months3 = Enumerable.Range(1, 12).Select
          (
             i => new KeyValuePair<int, string>
                      (
                           i
                         , System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i)
                      )
                ).ToDictionary
                (
                      x => x.Key
                    , x => x.Value
                );

 

Yona


string[] months4 = DateTimeFormatInfo.CurrentInfo.MonthNames;   

List<string> months4AsList = months4.ToList();


 

DivineOps


var months5 = DateTimeFormatInfo.CurrentInfo.MonthNames
		.Where
	         ( 
		     p=>!string.IsNullOrEmpty(p)
	         )
		.Select
                 (

		     (item, index) => new 
				      { 
					   monthNumber = index + 1
					 , monthName = item
				      }
		);

 

Maher Ben Issa


var months6 = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
                .TakeWhile
                 (
                     m => m != String.Empty
                 )
                 .Select
                  (
                     (m,i) => new  
                                {  
                                    monthNumber = i+1
                                  , monthName = m
                                }
                 ) 
                 .ToList(); 
   

 

Observation

Various StackOverflow contributors handle the empty entry in novel ways.

    1. mmmeff and Tiago Ávila
      • Enumerable.Range(1, 12)
    2. SeanH
      • Enumerable.Range(1, 12)
      • Dictionary Object
    3. Yona
      • Used the plain DateTimeFormatInfo.CurrentInfo.MonthNames
      • And, kept the empty last entry
    4. DivineOps
      • Filters out the empty entry using the where clause ( string.IsNullOrEmpty )
      • Sample Code
        var months5 = DateTimeFormatInfo.CurrentInfo.MonthNames
        	      .Where
                       ( 
                           p=>!string.IsNullOrEmpty(p)
                       )
        
    5. Maher Ben Issa
      • Filters out the empty entry by using the TakeWhile clause
      • Sample Code
        CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
        .TakeWhile
        (
           m => m != String.Empty
        )
        

 

Code Sharing

  1. GitLab
    • .Net – LINQ – calendar – month
      Link

On Line Lab

  1. OnlineGDB
    • .Net – LINQ – calendar – month
      Link

Summary

StackOverflow is a good thing.

Like Dela, sometimes you find mushiness in your fav programming language.

Hopefully someone hints before you knock your head against the wall too many times.

 

Dedicating

Dedicating to StackOverflow.

The engineering team and the unsurpassable contributors.

One thought on “.Net:- C# – Get List of Month Names

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s