且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在c#中将字符串转换为DateTime

更新时间:2023-02-03 08:03:10

试试这个:

DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

如果字符串的格式可能不正确(并且您希望避免出现异常),您可以像这样使用 DateTime.TryParseExact 方法:

If the string may not be in the correct format (and you wish to avoid an exception) you can use the DateTime.TryParseExact method like this:

DateTime dateTime;
DateTime.TryParseExact(str, "yyyyMMddHHmmss",
    CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);