Timezone removal for DateTime.ParseExact

I am trying to parse a string in datetime in the following format:

[Day], [Date] [Month] [Year] [Time] [am / pm] [time zone] (example :)

Thursday, December 1, 2011 20:30 EST

I did this using DateTime.ParseExact with the format ("dddd, dd MMM yyyy hh: mmtt"). However, the timbre is causing me a problem. There is no timezone code written this way. I still don’t worry about the time zone, so I want to either remove it or read it - while the parsexact will work.

One way to remove it is to actually remove it from the string (using .Replace) - however, I don't know how many different time intervals the source will generate, and in any case, I think the long replacement string looks ugly and the error is -prone.

So, is there a way to either delete it or read it (and then I can ignore it)?

+3
source share
1 answer

You can scan the line for spaces and cut everything after fifth place. If there are only four spaces, save the entire line (this means that there is no time zone).

This answer has been edited after a comment by Jon .

0
source

All Articles