【问题标题】:Convert a string to custom DateTime [duplicate]将字符串转换为自定义 DateTime [重复]
【发布时间】:2011-11-23 11:08:23
【问题描述】:

可能重复:
Convert string to datetime Using C#

我想将.NET 2.0 中的字符串转换为日期时间。例如,字符串将采用10/07/2011 (dd/MM/yyyy) 之类的格式,然后我想将其转换为 DateTime 对象。

【问题讨论】:

    标签: .net string datetime .net-2.0


    【解决方案1】:

    您可以使用 DateTime.ParseExact 将自定义字符串日期格式解析为 DateTime。 例如:

    var dateTime = DateTime.ParseExact(
        "10/07/2011",
        "dd/MM/yyyy",
        CultureInfo.InvariantCulture);
    

    或者您可以指定具有 DMY 日期格式的文化,例如英国:

    var dateTime = DateTime.Parse("13/07/2011", new CultureInfo("en-GB"));
    

    【讨论】:

      【解决方案2】:
      DateTime dt = DateTime.Parse(strdate); 
      

      【讨论】:

      • 谢谢,但我收到错误,因为“字符串未被识别为有效的日期时间”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2014-01-15
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多