【问题标题】:Converting DateTime string in ddd MMM dd HH:mm:ss 'EST' yyyy format?以 ddd MMM dd HH:mm:ss 'EST' yyyy 格式转换 DateTime 字符串?
【发布时间】:2017-08-06 06:59:22
【问题描述】:

在我看来,我正在返回一个字符串,该字符串正在我的控制器中转换为 DateTime。字符串格式为“ddd MMM dd HH:mm:ss 'EST' yyyy”。

我正在像这样成功转换它:

var startDate = DateTime.ParseExact(_startDate, "ddd MMM dd HH:mm:ss 'EST' yyyy", CultureInfo.InvariantCulture);
model.StartDate = Convert.ToDateTime(startDate);

问题是时区可以是任何东西。我该如何解释呢?并非总是如此,它会是 'EST' 。例如,如果它是 EDT,它将引发异常。

示例输入字符串为:Mon Feb 20 00:00:00 EST 2017

编辑:也可以是ddd MMM d HH:mm:ss 'EST' yyyy的格式 这只会在美国和加拿大时区使用。

【问题讨论】:

  • 你能提供一些示例输入
  • 当变量已经DateTime时,为什么要调用Convert.ToDateTime
  • @GlobalJim - 为什么不呢?除了这 3 个字符之外,您清楚地知道字符串的 exact 格式。只需将它们从_startDate..中取出即可。
  • 顺便说一句,ESTEDT 不是时区名称。唯一标准化的时区名称是 IANA 时区数据库中的名称。如果您使用 ISO8601 格式而不是使用这些缩写,那会容易很多

标签: c# asp.net-mvc datetime


【解决方案1】:

在我的控制器中,我现在执行以下操作。

 var startDateTZ = _startDate.Substring(20, 4);
        if (startDateTZ[3] == ' ')
        {
            startDateTZ = _startDate.Substring(20, 3);
        }
        if (startDateTZ.Length > 3)
        {
            if (startDateTZ[3] == '0')
            {
                startDateTZ = _startDate.Substring(19, 4);
            }
            if (startDateTZ[3] == '2')
            {
                startDateTZ = _startDate.Substring(19, 3);
            }
        }
        var startDate = new DateTime();
        if (_startDate[9] != ' ')
        {
             startDate = DateTime.ParseExact(_startDate, "ddd MMM dd HH:mm:ss '" + startDateTZ + "' yyyy", CultureInfo.InvariantCulture);
        }
        else
        {
             startDate = DateTime.ParseExact(_startDate, "ddd MMM d HH:mm:ss '" + startDateTZ + "' yyyy", CultureInfo.InvariantCulture);
        }
        model.StartDate = startDate;

【讨论】:

    【解决方案2】:

    我会引用here:

    快速回答是,你不能这样做。

    (有解释你不能的原因……这里就不复制了)

    所以我能给你的是一种将时区提取为string 的方法,然后你可以做任何你想做的事情(例如你可以有一个Dictionary<string, offset>... 这不是一个好主意,但是可能会更糟,但请参阅this comment俄罗斯在过去 4 年中多次更改 DST 规则

    public static bool TryParseDateTimeWithTimeZone(string s, out DateTime result, out string timeZone)
    {
        if (s == null)
        {
            throw new NullReferenceException("s");
        }
    
        int ixEnd = s.LastIndexOf(' ');
    
        if (ixEnd == -1 || ixEnd == 0)
        {
            throw new FormatException();
        }
    
        int ixStart = s.LastIndexOf(' ', ixEnd - 1);
    
        if (ixStart == -1)
        {
            throw new FormatException();
        }
    
        timeZone = s.Substring(ixStart + 1, ixEnd - ixStart - 1);
    
        string s2 = s.Remove(ixStart) + s.Substring(ixEnd);
    
        bool success = DateTime.TryParseExact(s2, "ddd MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
    
        if (!success)
        {
            timeZone = null;
        }
    
        return success;
    }
    

    使用

    DateTime dt;
    string tz;
    
    bool success = TryParseDateTimeWithTimeZone("Mon Feb 20 01:02:00 EST 2017", out dt, out tz);
    

    现在tx == "EST"dt2017-02-20 01:02:00Kind == Undefined

    其他有关缩写的答案:Timezone Abbreviations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多