C# get month number from month text

C# get month number from month text

You have month string like “may” and you want to get it’s integer value. Here I will show you how to do get month number from month text or string.

Month String to Int

using System;

namespace Csharp.Month.String.ToInt.Example
{
    class Program
    {
        static void Main(string[] args)
        {
          
          string month = "May";
          string year = "2020";
          string date = string.Format("{0} {1}",month,year);
          DateTime convertedDate = DateTime.Parse( date );
          
          Console.WriteLine( convertedDate.Month );
          Console.ReadLine();
              
        }
 
    }
 
}

In above example I have month string and It writes month number 5 which corresponds to May. Month property of DateTime instance returns integer value of that date’s month part.

Tags:

C#