user386258 user386258 - 1 month ago 9
C# Question

Best way to get the current month number in C#

I am using C# to get current month number:

string k=DateTime.Now.Month.ToString();


For January it will return
1
, but I need to get
01
. If December is the current month, I need to get
12
. Which is the best way to get this in C#?

Answer
string sMonth = DateTime.Now.ToString("MM");