Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中获取当前月数的最佳方法#_C#_String_Datetime - Fatal编程技术网

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

我正在使用C#获取当前月份编号:

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"));
    }
}