C# 在C中获取当前月数的最佳方法#
我正在使用C#获取当前月份编号:C# 在C中获取当前月数的最佳方法#,c#,string,datetime,C#,String,Datetime,我正在使用C#获取当前月份编号: string k=DateTime.Now.Month.ToString(); 对于一月,它将返回1,但我需要获得01。如果十二月是当前月份,我需要获得12。在C#中,哪一种方法是最好的方法?有很多不同的方法 为了保持语义,我将使用DateTime的Month属性,并使用以下格式之一: 输出 五, May如果我们需要其他月份的MM怎么办?@AlokRajasukumaran someDate.ToString(“MM yyyy”) string sMonth
string k=DateTime.Now.Month.ToString();
对于一月,它将返回
1
,但我需要获得01
。如果十二月是当前月份,我需要获得12
。在C#中,哪一种方法是最好的方法?有很多不同的方法
为了保持语义,我将使用DateTime
的Month
属性,并使用以下格式之一:
输出
五,
May如果我们需要其他月份的MM怎么办?@AlokRajasukumaran someDate.ToString(“MM yyyy”)
string sMonth = DateTime.Now.ToString("MM");
DateTime.Now.Month.ToString("00");
DateTime.Now.Month.ToString("0#")
using System;
class Program
{
static void Main()
{
//
// Get the current month integer.
//
DateTime now = DateTime.Now;
//
// Write the month integer and then the three-letter month.
//
Console.WriteLine(now.Month);
Console.WriteLine(now.ToString("MMM"));
}
}