Generate current month in C#

To generate the current month in C#, you can use the DateTime.Now property to get the current date and time, then extract the month information using various properties and formatting methods.

Syntax

Following is the syntax to get the current date −

DateTime dt = DateTime.Now;

Following is the syntax to get the month as a number −

int month = dt.Month;

Following is the syntax to get the month name using formatting −

string monthName = dt.ToString("MMMM");

Getting Current Month as Number

The Month property returns the month component as an integer value from 1 to 12 −

using System;

public class Demo {
    public static void Main() {
        DateTime dt = DateTime.Now;
        Console.WriteLine("Current month number: " + dt.Month);
        Console.WriteLine("Current date: " + dt.ToShortDateString());
    }
}

The output of the above code is −

Current month number: 12
Current date: 12/15/2024

Getting Current Month Name

You can use different format strings to display the month name in various formats −

using System;

public class Demo {
    public static void Main() {
        DateTime dt = DateTime.Now;
        Console.WriteLine("Short month name: " + dt.ToString("MMM"));
        Console.WriteLine("Full month name: " + dt.ToString("MMMM"));
        Console.WriteLine("Month with leading zero: " + dt.ToString("MM"));
        Console.WriteLine("Month without leading zero: " + dt.ToString("M"));
    }
}

The output of the above code is −

Short month name: Dec
Full month name: December
Month with leading zero: 12
Month without leading zero: 12

Common Month Formatting Options

Format String Description Example
M Month number without leading zero 1, 12
MM Month number with leading zero 01, 12
MMM Abbreviated month name Jan, Dec
MMMM Full month name January, December

Using DateTimeOffset for Current Month

You can also use DateTimeOffset which includes timezone information −

using System;

public class Demo {
    public static void Main() {
        DateTimeOffset currentTime = DateTimeOffset.Now;
        Console.WriteLine("Current month: " + currentTime.Month);
        Console.WriteLine("Current month name: " + currentTime.ToString("MMMM"));
        Console.WriteLine("Full date with timezone: " + currentTime.ToString());
    }
}

The output of the above code is −

Current month: 12
Current month name: December
Full date with timezone: 12/15/2024 10:30:45 AM -05:00

Conclusion

Getting the current month in C# is straightforward using DateTime.Now.Month for the numeric value or DateTime.Now.ToString() with format strings like "MMM" or "MMMM" for month names. The DateTime class provides flexible formatting options to display month information in various formats.

Updated on: 2026-03-17T07:04:35+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements