Removing Timezone for DateTime.ParseExact

…衆ロ難τιáo~ 提交于 2020-01-13 17:07:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!