I am trying to parse a datetime value using this:
I am getting this error:
String was not recognized as a valid DateTime.
Any help would be appreciated.
4 Answers 4
you should write:
because hh means 12h time and HH means 24h time and putting 23 as hour in 12h time is invalid 🙂
Of course if you are sure that hours are second in your time and you don’t want to write HH:mm:ss or hh:mm:ss (for 12h format)
I have a datetime coming back from an XML file in the format:
How can i get the datetime.parse function to pick up on this? Ie parse it without erroring? Cheers
2 Answers 2
assuming you meant to say that minutes followed the hours, not seconds — your example is a little confusing.
The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that.
As @Joel Coehoorn mentions, there’s also the option of using TryParseExact, which will return a Boolean value indicating success or failure of the operation — I’m still on .Net 1.1, so I often forget this one.
If you need to parse other formats, you can check out the Standard DateTime Format Strings.
У меня есть дата, представленная в виде строки, поэтому
Я назвал DateTime.Parse, но это не удалось. Как я могу преобразовать это в DateTime? Кстати, часовой пояс — это CET.
EDIT
Предлагаемые решения очень полезны до сих пор, но похоже, что они не поддерживают 24-часовые часы, все еще ищут решение, которое делает.
Источник: