如何转换它,错误显示“字符串未被识别为有效日期时间” [英] how to convert this,the error show "string was not recognized as a valid datetime"

查看:191
本文介绍了如何转换它,错误显示“字符串未被识别为有效日期时间”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (userDateFormat.Trim().Contains("dd/MM/yyyy"))
    convertedDateTime = DateTime.ParseExact(dateTimeString,
        format_dmy,
        CultureInfo.InvariantCulture,
        DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);

推荐答案

假设第二行是抛出异常的那一行,你应该可能使用更安全的错误检查版本的DateTime解析器...



https://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact%28v=vs.110%29.aspx [ ^ ]



这将允许您加入我们e相同的值,但是您将convertedDateTime的实例作为方法中的最终参数传递为out convertedDateTime,并且您可以检查返回的布尔值以查看转换是否成功。
Assuming that the second line is the one that threw the exception, you should probably use the safer error checking version of the DateTime parser...

https://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact%28v=vs.110%29.aspx[^]

This will allow you to use the same values, but you pass the instance of your convertedDateTime as the final parameter in the method as "out convertedDateTime" and you can check the returned Boolean value to see if your conversion was successful.


这篇关于如何转换它,错误显示“字符串未被识别为有效日期时间”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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