The following example code demonstrate us how can we get the month part/component from a dateTime object programmatically at run time. .Net framework’s DateTime.Month property allow us to get the month part of the date represented by this instance. DateTime is exists in .Net Framework’s System namespace.
DateTime.Month property return a System.Int32 data type value. This return integer number represent the month component of the provided DateTime instance. This return integer value is a number between 1 to 12 where 1 represent January, 2 represent February and so on. The provided output image help you more to better understand this tutorial.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Program { static void Main(string[] args) { DateTime myDateTime = DateTime.Now; string month = myDateTime.Month.ToString(); Console.WriteLine("My DateTime: " + DateTime.Now.ToLongDateString()); Console.WriteLine("Month: " + month); Console.ReadLine(); } } |
Output: