views:

132

answers:

6
DateTime dt = Convert.ToDateTime(dateTimePicker1.Text); //taken the DateTime from form
string dt1 = String.Format("Y", dt); //trying to make so that it comes to "August 2009"

Tried but all I get is dt1="Y".

+4  A: 
DateTime dt = Convert.ToDateTime(dateTimePicker1.Text); //taken the DateTime from form
string dt1 = dt.ToString("MMMM yyyy")
Gordon Bell
A: 

Try dt.ToString("Y")

guardi
+1  A: 

Try:

string dt1 = dt.ToString("MMMM yyyy");
Callum Rogers
+3  A: 
DateTime dt = Convert.ToDateTime(dateTimePicker1.Text); //taken the DateTime from form
string dt1 = String.Format("{0:MMMM yyyy}", dt); //trying to make so that it comes to "August 2009"
Guster_Q
This is the correct way using String.Format.
Scott Dorman
+2  A: 

You have other answers that use ToString .. if you want to use string.Format, try this:

string dt1 = string.Format("{0:Y}", dt);
Ben M
+2  A: 

The other answers will work. I just wanted to comment on the reason why your code doesn't work. String.Format is designed to format more than 1 variable at the same time. You could use it, but the syntax would be:

string str = String.Format("{0:Y}", dt);

hope that helps. The format specifier you're looking for is probably "MMMM yyyy", the default format for "Y" is year-month, not month-year.

Thorarin