C#TryParseExact读取时区 [英] C# TryParseExact Reading Time Zone

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

问题描述


可能重复:

解析使用PST / CEST / UTC / etc形式的时区的日期时间

我有一个以下格式的日期字符串

I have a date string in the following format

Fri, 14 Dec 2012 6:52 am PST

我想将其转换为日期。我使用 DateTime.TryParseExact 来实现它,但是我无法读取时区。如果我将字符串(没有时区)更改为

I want to convert it to date time. I am using DateTime.TryParseExact to achieve it, but I am not able to read the time zone. If I change the string (without the time zone) to

Fri, 14 Dec 2012 6:52 am

并使用

DateTime.TryParseExact(DateString, "ddd, dd MMM yyyy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None,out dt)

它可以工作。
我如何读取时区。

it works. How can I read the time zone.

编辑 - 我正在从Yahoo weather API获取数据(例如 http://weather.yahooapis.com/forecastrss?w=2420380 )。

Edit - I am getting the data from the Yahoo weather API (e.g. http://weather.yahooapis.com/forecastrss?w=2420380).

谢谢。

推荐答案

我不相信有一个自定义日期和时间格式说明符,它处理时区(而不是当前的UTC偏移量)。即使有时区缩写也可能含糊不清,因此通常情况下,传播时区信息的方式很差。不幸的是,他们被广泛使用,当然...

I don't believe there's a custom date and time format specifier which handles time zones (as opposed to current UTC offsets). Even if there were, time zone abbreviations are potentially ambiguous, so they're generally poor as a way of communicating time zone information. Unfortunately they're widely used, of course...

我怀疑你最好创建一个从缩写到最佳猜测时区的地图,然后剥离任何从输入字符串的最后一个空格之后,并使用它来获取区域。

I suspect you'll be best off creating a map from abbreviation to "best guess time zone", then stripping whatever's after the last space from your input string, and using that to get the zone.

为了防万一有人想知道, Noda时间也没有处理这个 - 但我希望在某些时候取得进展:)

Just in case anyone was wondering, Noda Time doesn't handle this yet either - but I'm hoping to make progress on it at some point :)

这篇关于C#TryParseExact读取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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