Convert.DateTime
What Convert.DateTime will convert the date开发者_StackOverflow中文版 7/25/2010 12:00:00 it's current format is(MM/dd/yyyy HH:mm:ss)?
When I convert this string format to date time I am getting the error "string was not recognized as valid DateTime"
None. Dates are not stored internally as a certain format.
If you want to parse a string into a date, use DateTime.ParseExact
or DateTime.TryParseExact
(the former will throw an exception if the conversion fails, the second uses an out
parameter):
DateTime myDate = DateTime.ParseExact("7/25/2010 12:00:00",
"MM/dd/yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
If you want to display a certain format, use ToString
with the format string.
So, if you have a date object that represents midday of the 25th of July 2010 (doesn't matter how it is represented internally) and you want to format it with the format string "MM/dd/yyyy HH:mm:ss" you do the following:
string formattedDate = myDate.ToString("MM/dd/yyyy HH:mm:ss");
If you need to use Convert.DateTime, I'll assume you're working with a string you want to convert to a date. So you might try this:
DateTime date = Convert.DateTime("7/25/2010 12:00:00 am");
string formattedDateString = date.ToString("MM/dd/yyyy HH:mm:ss")
I'm making no assumptions as to why you'd want to do this, except that, well, you have your reasons.
DateTime.TryParse()
or DateTime.Parse()
will do the trick.
Edit: This is assuming that you are going from a string to a DateTime object.
Edit2: I just tested this with your input string, and I receive no error with DateTime.Parse
精彩评论