【问题标题】:How to parse a string input to a DateTime?如何将字符串输入解析为 DateTime?
【发布时间】:2014-10-09 17:57:31
【问题描述】:

我已经弄清楚如何将输入字符串解析为日期时间对象。

但是如果我输入字符串并运行启动计时器的方法然后停止它,我无法重新编辑字符串输入而不会出现格式异常。

在测试中我输入:"00 : 00 : 10 : 000",然后启动我的计时器和秒表,但是当我对两者都调用 stop 并尝试为字符串输入一个新值时,例如"00 : 00 : 22 : 000" 它给了我以下异常:

An exception of type 'System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: String was not recognized as a valid DateTime.

这是将字符串解析为日期时间的方式:

            //Assign text box string value to a date time variable.
            DateTime workDt = DateTime.ParseExact(wrkString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);
            DateTime restDt = DateTime.ParseExact(rstString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);

有没有办法在代码中处理这种类型的输入异常,或者我在解析字符串时可能缺少一个额外的步骤?

【问题讨论】:

  • 如果您使用时间间隔,您可以使用 TimeSpan 而不是 DateTime。
  • 您的“DateTime”字符串不包含日期信息。正如 Luizgrs 建议的那样,使用 TimeSpan

标签: c# string datetime stopwatch formatexception


【解决方案1】:

{这是评论,不是答案,但我需要正确格式化。}

一定有一些其他信息导致了您未提供的问题。这对我有用:

string s= "00 : 00 : 10 : 000";
DateTime workDt = DateTime.ParseExact(s, "HH : mm : ss : fff", CultureInfo.InvariantCulture);
s= "00 : 00 : 22 : 000";
DateTime restDt = DateTime.ParseExact(s, "HH : mm : ss : fff", CultureInfo.InvariantCulture);

但是,由于您只处理时间数据,因此最好使用 TimeSpan 代替:

string s= "00 : 00 : 10 : 000";
TimeSpan workTm = TimeSpan.ParseExact(s, @"hh\ \:\ mm\ \:\ ss\ \:\ fff", CultureInfo.InvariantCulture);
s= "00 : 00 : 22 : 000";
TimeSpan restTm = TimeSpan.ParseExact(s, @"hh\ \:\ mm\ \:\ ss\ \:\ fff", CultureInfo.InvariantCulture);

请注意,使用TimeSpan.Parse 时,冒号和空格需要转义。

【讨论】:

  • 我已经尝试使用 TimeSpan 而不是 DateTime 进行上述操作,但没有 TimeOfDay 属性来比较经过的时间。使用 TimeSpan 时可以使用什么等价属性?
  • 目前如果(myStopwatch.Elapsed >= workDt.TimeOfDay),我可以将它与 DateTime 一起使用,但我怎样才能通过 TimeSpan 实现相同的效果?
  • @BrianJ 没有一个 - 时间跨度是一个持续时间,而不是一个时间点。您应该能够直接比较两个 TimeSpan 值:if (myStopwatch.Elapsed >= workDt)
  • 上述解决方案对我有用,我按照建议决定了时间跨度。在我的实现中,它允许用户输入而不是常量值字符串。如何防止用户在文本框中输入非数字或字母输入?例如,“00:00:k0:#00”;
【解决方案2】:

试试 Convert 类:

myDateAsString="3/29/2014";
try
{
Convert.ToDate(myDateAsString)
}
catch(Format exception)
{
//do something
}

这是另一种方法,我同意这一点,但我认为它更容易,我希望它有所帮助:)

【讨论】:

    【解决方案3】:

    如果您知道可能会出错,我建议您使用 TryParseExact 方法。

    我还建议在处理时间间隔时使用 TimeSpan 而不是 DateTime。无论如何,该方法也存在于 DateTime...

    TimeSpan ts;
    if (TimeSpan.TryParseExact(wrkString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture, out ts))
    {
       //ts formatted successfully
    }
    else
    {
        //failure
    }
    

    【讨论】:

      【解决方案4】:

      您也可以使用DateTime.TryParse进行转换。

      DateTime dateValue;
      string[] dateStrings = "1/1/2014";
      if (DateTime.TryParse(dateString, out dateValue)) 
      {
      //code
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-08
        • 2011-11-26
        • 2020-09-27
        • 2013-06-23
        • 1970-01-01
        相关资源
        最近更新 更多