Go to home page mail me! RSS Feed
FoxMetrics Web Analytics

C# - Get short month name from month int value

Monday, July 02, 2007 9:52 AM

 Ok, I've been asked one too many times about how to convert a month int value to a short or long month name - so here goes nothing.

 

   1: /// <summary>
   2: /// Start program
   3: /// </summary>
   4: /// <param name="args">Arguaments not required</param>
   5: static void Main(string[] args)
   6: {
   7:     for (int month = 1; month < 13; month++)
   8:     {
   9:         System.Console.WriteLine(GetMonthName(month, true));
  10:     }
  11:  
  12:     System.Console.ReadKey();
  13: }
  14:  
  15: /// <summary>
  16: /// Converts the given month int to month name
  17: /// </summary>
  18: /// <param name="month">month in</param>
  19: /// <param name="abbrev">return abbreviated or not</param>
  20: /// <returns>Short or long month name</returns>
  21: private static string GetMonthName(int month, bool abbrev)
  22: {
  23:     DateTime date = new DateTime(1900, month, 1);
  24:     if (abbrev) return date.ToString("MMM");
  25:     return date.ToString("MMMM");
  26: }

Your Comments.

  • # re: C# - Get short month name from month int value

    Gravatarvery good, short and precisely what i needed. Used to use multiple select case statements, but i always knew it was the wrong way to go about it.

    Left by Stephen at 6/4/2008 5:21 AM
  • # re: C# - Get short month name from month int value

    Gravatar'' Instead of executing please try this code
    = MonthName(1) ' Will return january

    Left by Rajasekar P at 8/19/2008 9:41 AM
  • # re: C# - Get short month name from month int value

    Gravatargood

    Left by eded at 5/6/2009 3:54 AM
  • # re: C# - Get short month name from month int value

    Gravatarujhgjhgjhg hg gj

    Left by ghfghfghfgh at 10/10/2009 5:00 AM
  • # re: C# - Get short month name from month int value

    GravatarQuite inspiring,

    Thanks

    Left by UK Software company at 11/26/2009 7:25 AM

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 1 and 3 and type the answer here:

Preview Your Comment.

 
Next entries »