Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Extracting month in a given date in C#

The code below extracts the month in a given date.

  1. using System;
  2.  
  3. namespace forgetCode
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9.  
  10. Console.WriteLine("Enter year in the format dd-mm-yyyy");
  11. DateTime dt = Convert.ToDateTime(Console.ReadLine());
  12.  
  13. //Know the month
  14.  
  15. int month = dt.Month;
  16.  
  17. Console.WriteLine("The month in the date you entered: "+month);
  18. }
  19.  
  20. }
  21. }


Output:
Enter year in the format dd-mm-yyyy
03-12-2012
The month in the date you entered: 12

..