Bruce
Bruce

Reputation: 2203

converting to datetime using Convert.ToDateTime

I have a "string" in format mm/dd/yyyy and want to change this to "Feb 2, 2011".

Can this be done using Convert.ToDateTime?

Thanks Behrouz

Upvotes: 0

Views: 11804

Answers (5)

Crimsonland
Crimsonland

Reputation: 2204

Try This:

string test = "1/21/2011";

string result = Convert.ToDateTime(test).ToLongDateString();

MessageBox.Show(result);

Regards!

Upvotes: 0

Danil
Danil

Reputation: 1893

You need to convert your string to DateTime First and then Convert to string using required format

 String.Format("{0:MMM d, yyyy}", Convert.ToDateTime(date));

Here is link

Upvotes: 4

gizgok
gizgok

Reputation: 7639

Use Convert.ToDateTime(string date,IFormatProvider provider) where the format provider should be "{0:dddd, MMMM d, yyyy}".

Upvotes: 0

rsbarro
rsbarro

Reputation: 27339

This code should work:

var dateString = "02/02/2011";
Console.WriteLine(DateTime.Parse(dateString).ToString("MMM d, yyyy"));

Upvotes: 1

Jobi Joy
Jobi Joy

Reputation: 50038

 DateTime dateObject= DateTime.Parse(yourDateString);
 dateObject.ToString("MMMM dd, yyyy")

or in single line

string result = DateTime.Parse(yourDateString).ToString("MMMM dd, yyyy");

Upvotes: 3

Related Questions