I have following 开发者_开发问答string "16:07:57.796"
how i can parse it to this 6/18/2011 16:07:57
?
If I just try to parse it DateTime.Parse("16:07:57.796")
I get 6/18/2011 04:07:57 PM
And it's not what I need.
Thanks for help.
It is parsed correctly (as 4 pm is 16) but it is your locale information which displays it differently than you want. You should use the following ToString method, which takes an IFormatProvider, in which you can pass in a CultureInfo that fits you.
Otherwise you can format your string using custom date and time formats like the following:
date.ToString("M/dd/yyyy HH:mm:ss")
It looks like it's parsing the string fine. You just want to display it in 24-hour format instead of 12-hour format (16:07:57 is the same as 4:07:57 PM). Try something like DateTime.Parse("16:07:57.796").ToString("M/d/yyyy H:mm:ss")
.
It's correctly parsing it to the specified time on the current date.
If you don't want the current date, you can use the overload that takes a DateTimeStyles
parameter, and specify DateTimeStyles.NoCurrentDateDefault
. In this case, the date will be 01/01/0001.
精彩评论