C#: Get Current Month Name and Short Name

We had a requirement to get the current month name and its short name (example- November|Nov). While DateTime class does not provide these methods out of .net framework, we had to create methods which gives the similar output.. We have created similar methods through Extension Methods supported with recent .net framework.

Extension methods can be used as instance methods and it uses “this” keyword as a parameter. They are static and they must be defined on static class.

Here is the solution:

using System;
using System.Globalization;

namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Today.Day + "." + DateTime.Today.Month + "." + DateTime.Today.Year);
            Console.WriteLine(DateTime.Now.AddMonths(-2).ToMonthName());
            Console.WriteLine(DateTime.Now.ToShortMonthName());
            
            // Update: Similar functionality can be achieved through below statements also.
            // Thanks Michael for inputs.
            Console.WriteLine(String.Format("{0:MMMM}", DateTime.Now));
            Console.WriteLine(String.Format("{0:MMM}", DateTime.Now));
            Console.WriteLine(DateTime.Now.ToString("MMMM"));
            Console.WriteLine(DateTime.Now.ToString("MMM"));
        }
    }

    static class DateTimeExtensions
    {
        public static string ToMonthName(this DateTime dateTime)
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month);
        }

        public static string ToShortMonthName(this DateTime dateTime)
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(dateTime.Month);
        }
    }
}

Output:
28.11.2014
September
Nov

Extension Methods in Visual Studio appears with special symbol:

Extension Methods

Extension Methods

Reference: http://www.dotnetperls.com/extension

Advertisements

3 thoughts on “C#: Get Current Month Name and Short Name

  1. Why don’t you use:

    Console.WriteLine(String.Format(“{0:MMMM}”, DateTime.Now));
    Console.WriteLine(String.Format(“{0:MMM}”, DateTime.Now));
    Console.WriteLine(DateTime.Now.ToString(“MMMM”));
    Console.WriteLine(DateTime.Now.ToString(“MMM”));

    • Hi Michael,

      Thank you very much for the inputs and information, I was totally unaware of these methods.

      I wanted to illustrate Extension Methods related to similar functionality. I am going to add the code to the post now.

      Regards,
      Rikin

Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Google+ photo

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

Connecting to %s