1

I'm getting the current month and year from the dateTime and passing it to a string. The reason for this is, in my directory there has to be a folder for the current year and month - this is done in order for video uploading quantity. This is what I currently have:

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

All this works, but the problem is, is that month displays at 5 which is correct, but I need it to display May. How do I do this?

Kerieks
  • 1,042
  • 7
  • 25
  • 53

6 Answers6

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

or

DateTime.Now.ToString("MMM");

Depending if you want January or Jan

Paulie Waulie
  • 1,690
  • 13
  • 23
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");
Richard Pursehouse
  • 1,109
  • 13
  • 21
  • 1
    Surprised at the answer not getting upvotes, This is the first and correct answer so +1 – Habib May 15 '13 at 12:12
3

try something like

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

For Advance Knowledge Click here

Hope it works.

Rahul
  • 5,603
  • 6
  • 34
  • 57
2

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

kalbsschnitzel
  • 817
  • 1
  • 8
  • 29
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

Matan L
  • 997
  • 3
  • 14
  • 35
1
string month= DateTime.Now.ToString("MMM", CultureInfo.InvariantCulture);
Akrem
  • 5,033
  • 8
  • 37
  • 64