删除DateTime.ParseExact的时区 [英] Removing Timezone for DateTime.ParseExact

查看:64
本文介绍了删除DateTime.ParseExact的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串解析为以下格式的日期时间:

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

[Day],[Date] [Month] [Year] [Time] [ am / pm] [时区](示例:)

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

2011年12月1日星期四,美国东部标准时间

"Thursday, 1 Dec 2011 08:30pm EST"

我使用DateTime.ParseExact格式( dddd,dd MMM yyyy hh:mmtt)完成了此操作。但是,timzone给了我一个问题。没有用于读取以这种方式编写的时区的代码。无论如何,我都不在乎时区,所以我想将其剥离或阅读-只要能使用仿冒品即可。

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.

一种删除方式是实际上是从字符串中删除它(使用.Replace)-但是我不知道源会产生多少个不同的时区,无论如何,我认为一长串替换看起来很丑陋且容易出错。

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) ?

推荐答案

您可以扫描字符串中的空格,并在第五个空格后剪切所有内容。如果只有四个空格,则保留整个字符串(这意味着没有时区)。

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).

此答案已由乔恩

这篇关于删除DateTime.ParseExact的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆