解析非常长的日期格式到C#中的DateTime [英] Parse very long date format to DateTime in C#

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

问题描述

如何解析以下字符串日期到C#中的DateTime对象:

How would I parse the following string date to a DateTime object in C#:

1970年1月1日星期四

"Thursday, 1st January 1970"

这是来自一个XML feed和DateTime.Parse似乎并不喜欢en-GB locale。这个饲料只会来自英国的一个服务器,所以我不用担心全球化问题。

This is coming from an XML feed and DateTime.Parse doesnt seem to like it in en-GB locale. The feed will only ever come from a british server so I'm not to worried about globalization issues

我最初的暴力手段将是:

My initial brute force approach would be to:


  • 删除所有包括逗号在内的所有内容,并留下1970年1月1日的尾随空格

  • 然后删除 st,nd,rd或th适当地离开1970年1月1日

  • 然后将月份转换为数字等价物,离开1 1 1970

  • 然后用/替换空格来获取1/1/1970

  • Delete everything up to and including the comma, and the trailing space to leave "1st January 1970"
  • Then remove "st", "nd", "rd" or "th" as appropriate to leave "1 January 1970"
  • Then convert month to its numeric equivalent leaving "1 1 1970"
  • Then replace spaces with "/" to get "1/1/1970"

我确定那里一定是一个更优雅的方式吗?我不能得到DateTime.Prse或Datetime.ParseExact工作

Im sure there must be a far more elegant way though? I couldnt get DateTime.Prse or Datetime.ParseExact to work

推荐答案

只是提供一个略有不同的采取,并给你你有一些其他选择的想法;您可以将DateTime.Parse(或TryParse)格式指定为我的示例,以解决这种情况,而不尝试将字符串预格式化为其他与 String.Replace 调用等;

Just to provide a slightly different take on this, and give you an idea of some other options you have; you can specify the format to DateTime.Parse (or TryParse as in my example) to account for circumstances like this, without trying to 'pre format' the string into something else with String.Replace calls and the like;

public DateTime ParseOrdinalDateTime(string dt)
{
    string[] expectedFormats = 

    DateTime d;
    if (DateTime.TryParseExact(dt, "dddd, d\"st\" MMMM yyyy", null, DateTimeStyles.None, out d))
        return d;
    if (DateTime.TryParseExact(dt, "dddd, d\"nd\" MMMM yyyy", null, DateTimeStyles.None, out d))
        return d;
    if (DateTime.TryParseExact(dt, "dddd, d\"rd\" MMMM yyyy", null, DateTimeStyles.None, out d))
        return d;
    if (DateTime.TryParseExact(dt, "dddd, d\"th\" MMMM yyyy", null, DateTimeStyles.None, out d))
        return d;

    throw new InvalidOperationException("Not a valid DateTime string");
}

我提出这个方法的原因是它规定了你的输入期望非常清楚,并且包含单一方法的行为。如果格式发生变化,您可以在这里指定一个不同的格式字符串,并记录一个新的日期时间字符串结构。

The reason I'd propose this approach is that it sets out your input expectations very clearly, and contains the behaviour to a single method. If the format changes, you can just specify a different format string in here and account for a new date time string structure.

或者,上述略有变化考虑下面的意见;

Or, a slight variation on the above, taking into account below comments;

private static DateTime ParseOrdinalDateTime(string dt)
{
    string[] expectedFormats = new[]
    {
        "dddd, d'st' MMMM yyyy",
        "dddd, d'nd' MMMM yyyy",
        "dddd, d'rd' MMMM yyyy",
        "dddd, d'th' MMMM yyyy"
    };

    try
    {
        return DateTime.ParseExact(dt, expectedFormats, null, DateTimeStyles.None);
    }
    catch (Exception e)
    {
        throw new InvalidOperationException("Not a valid DateTime string", e);
    }
}

注意:唯一的原因我抓住并抛出一个InvalidOperationException以上是为了保护调用者不必得到 catch异常来处理任何可能的异常, DateTime.ParseExact 可能会抛出。您可以轻松修改此API。

NOTE: The only reason I catch and throw an InvalidOperationException above is to protect the caller from having to catch Exception to handle whatever possible exceptions DateTime.ParseExact may throw. You could easily modify this API.

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

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