1

我从 dateTime 获取当前月份和年份并将其传递给字符串。这样做的原因是,在我的目录中必须有一个当前年份和月份的文件夹 - 这样做是为了视频上传数量。这是我目前拥有的:

    string month = Convert.ToString(DateTime.Today.Month);
        string Year = Convert.ToString(DateTime.Today.Year);
        UploadStatusLabel.Text = Year + "\\" + month;
        //New Directory Name in string variable
        string NewDirectory = Server.MapPath("~\\uploads\\" + Year + "\\" + month);
        //Calling the function to create new directory
        CreateDirectoryIfNotExists(NewDirectory);

所有这些都有效,但问题是,月份显示为 5,这是正确的,但我需要它来显示 May。我该怎么做呢?

4

6 回答 6

4
DateTime.Now.ToString("MMMM");

or

DateTime.Now.ToString("MMM");

Depending if you want January or Jan

于 2013-05-15T11:48:21.590 回答
3

try something like

string month = DateTime.Now.ToString("MMMM");

For Advance Knowledge Click here

Hope it works.

于 2013-05-15T11:47:57.500 回答
3

Look at formatting date time into strings: http://www.csharp-examples.net/string-format-datetime/

This should be what you require for displaying the month 'May':

Datetime.Now.ToString("MMMM");
于 2013-05-15T11:46:43.310 回答
2

Does DateTime.Now.ToString("MMM") work for you?

于 2013-05-15T11:47:26.443 回答
2

Just use the ToString() method over the DateTime object, For example :

Datetime.Now.ToString("yyyy"); // will give you 2013
Datetime.Now.ToString("MMMM"); //will give you 'May'

Here is a reference : http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

于 2013-05-15T11:47:54.493 回答
1
string month= DateTime.Now.ToString("MMM", CultureInfo.InvariantCulture);
于 2013-05-15T11:48:04.190 回答