问题
I am trying to parse a string into a datetime with the following format:
[Day],[Date] [Month] [Year] [Time][am/pm] [timezone] (example:)
"Thursday, 1 Dec 2011 08:30pm EST"
I've done this using a DateTime.ParseExact with the format("dddd, dd MMM yyyy hh:mmtt"). However the timzone is giving me an issue. There is no code for reading the timezone written in that manner. I don't care about the timezone anyway, so I want to either strip it out or read it - as long as the parsexact will work.
One way of removing it is to actually remove it from the string (using .Replace) - however I don't know how many different timezones the source will produce, and anyway I think a long line of replace looks ugly and error-prone.
So is there a way of either removing it, or reading it (and then I can ignore it) ?
回答1:
You can scan the string for spaces, and cut everything after the fifth space. If there are only four spaces, keep the entire string (this means that there is no timezone).
This answer has been edited after a comment by Jon.
来源:https://stackoverflow.com/questions/10699440/removing-timezone-for-datetime-parseexact