在C#,.Net 4.0中解析RFC1123格式的日期 [英] Parsing RFC1123 formatted dates in C#, .Net 4.0

查看:189
本文介绍了在C#,.Net 4.0中解析RFC1123格式的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以RFC1123格式解析日期(Thu,2010年1月21日17:47:00 EST)。

I am trying to parse dates in RFC1123 format (Thu, 21 Jan 2010 17:47:00 EST).

这是我尝试但没有工作:

Here is what I tried but none worked:

DateTime Date = DateTime.Parse(dt);
DateTime Date = DateTime.ParseExact(dt, "r", null);

你能帮我一个吗?

谢谢,
Ruby:)

Thanks, Ruby :)

推荐答案

你是否尝试过如下:

string dateString, format;  
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

dateString = "Thu, 21 Jan 2010 17:47:00 EST";
format = "ddd, dd MMM yyyy hh:mm:ss EST";

result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());

我还没有测试(会在一会儿)...但我相信将为您做。

I haven't tested it yet (will in a few moments)... but I believe that will do it for you.

编辑:似乎问题是RFC1123表示时区应该始终是GMT ...这就是为什么r或R没有为您工作的格式。问题是EST。上面的模式描述了EST,但它是静态的,所以如果你有任何其他时区,你可能会遇到麻烦。最好的解决方案是使用RFC1123标准,并转到GMT,它应该可以解决你的问题。如果你不能,让我知道我可能会有一个解决方案。

It seems that the problem is that RFC1123 states that the timezone should always be GMT... which is why r or R did not work as a format for you. The problem is the EST. The pattern above accounts for EST, but it is static so if you have any other timezone you might be in trouble. The best solution would be to go with the RFC1123 standard and go to GMT and it should solve your problem. If you can't, let me know I might have a solution.

编辑2:这不是一个完整的解决方案,但它是如何隔离时区,仍然允许你解析它该代码不知道它正在呈现的时区,但是您可以在其上抛出任何时区缩写,它将解析时间。如果要转换为GMT,然后使用r或R,则可以使用正则表达式匹配的结果,将其放在查找表中(查看该时区缩写的时间偏移量),然后将时间转换为GMT和从那里解析这将是一个很好的解决方案,但更多的工作。这里是代码:

Edit 2: This is not a complete solution but what it does it isolates the timezone and still allows you to parse it. The code doesn't know the timezone that it is being presented with but you can throw any timezone abbreviation at it and it will parse the time. If you want to convert to GMT and then use r or R you can take the result of the regex match, put it against a lookup table (to see what the time offset it for that timezone abbreviation), then convert the time to GMT and parse from there. That would be a good solution but a little more work. Here's the code:

string dateString, format, pattern, tz;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
pattern = @"[a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ (?<timezone>[a-zA-Z]+)";
dateString = "Thu, 21 Jan 2010 17:47:00 EST";

Regex findTz = new Regex(pattern, RegexOptions.Compiled);

tz = findTz.Match(dateString).Result("${timezone}");

format = "ddd, dd MMM yyyy HH:mm:ss " + tz;

try
{
    result = DateTime.ParseExact(dateString, format, provider);
    Console.WriteLine("Timezone format is: {0}", format);
    Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
    Console.WriteLine("{0} is not in the correct format.", dateString);
}

    Console.ReadLine();

如果您想将其转换为时区转换器,以下是UTC偏移列表:

Here is a list of UTC offsets for you if you would like to turn this into a timezone converter:

使用UTC抵消的时区缩略语

这篇关于在C#,.Net 4.0中解析RFC1123格式的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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