C# Datetime

Associate
Joined
30 Mar 2004
Posts
1,148
Location
West Wing
How do I show the month in the MM format?

For example,
Code:
            DateTime now = DateTime.Now;
            int month = DateTime.Now.Month;

This shows the month as 5 but i need it as 05.

Thanks!
 
If you need leading zeroes you need to use a formatted string instead of an int:

Code:
string month = DateTime.Now.ToString("MM"); // MM = month with leading zero
Or
Code:
string month = DateTime.Now.Month.ToString("00");
Or
Code:
string month = DateTime.Now.Month.ToString().PadLeft(2, "0");
 
Back
Top Bottom