converting to datetime using Convert.ToDateTime
I have a "string" in format mm/dd/yyyy and want to change t开发者_Python百科his to "Feb 2, 2011".
Can this be done using Convert.ToDateTime?
Thanks Behrouz
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
DateTime dateObject= DateTime.Parse(yourDateString);
dateObject.ToString("MMMM dd, yyyy")
or in single line
string result = DateTime.Parse(yourDateString).ToString("MMMM dd, yyyy");
This code should work:
var dateString = "02/02/2011";
Console.WriteLine(DateTime.Parse(dateString).ToString("MMM d, yyyy"));
Use Convert.ToDateTime(string date,IFormatProvider provider) where the format provider should be "{0:dddd, MMMM d, yyyy}".
Try This:
string test = "1/21/2011";
string result = Convert.ToDateTime(test).ToLongDateString();
MessageBox.Show(result);
Regards!
精彩评论