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

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

问题描述

我试图分析在RFC1123格式(星期四,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);

你能帮我这个?

谢谢,
红宝石:)

Thanks, Ruby :)

推荐答案

你有没有尝试过这样的:

Have you tried something like:

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 ...这就是为什么R1或R2没有为你的格式工作。问题是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:这不是一个完整的解决方案,但它的作用是隔离的时区,仍允许您解析它。在code不知道它正在presented的时区,但你可以在它抛出任何时区的缩写,它会分析的时间。如果你想转换为GMT,然后用R1或R2可以采取正则表达式匹配的结果,把它与一个查找表(看什么时间偏移它的时区的缩写),然后将其转换时间为格林尼治标准​​时间和从那里解析。这将是一个很好的解决方案,但更多一点的工作。这里的code:

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:

时区缩写

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

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