本文介绍了如何使用tryparseexact来比较datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello All, 我需要检查字符串(将作为'YYYYMMDD'传递,例如20170413)是未来日期与否。我需要为此使用TryParseExact吗? 我目前使用
Hello All, I need to check the string(will be passed as 'YYYYMMDD' e.g. 20170413) is future date or not. I need to make use of TryParseExact for this? I have used
ParseExact它工作正常。 但现在我必须使用TryParseExact。 有人可以提供帮助吗? 提前致谢。 我尝试过: 尝试使用
currently and it is working fine. But now I have to use TryParseExact instead. Can anyone help in this? Thanks in advance. What I have tried: Tried using
ParseExact推荐答案
我猜您使用过 TryParse 并询问如何使用 TryParseExact 。 阅读文档: DateTime.TryParseExact Method(String,String,IFormatProvider,DateTimeStyles,DateTime)(System) [ ^ ]。 'YYYYMMDD'的格式字符串为yyyyMMdd。 提供者应为 CultureInfo.InvariantCulture 。 样式应为 DateTimeStyles.None 。 所以使用 I guess you have used TryParse and ask how to use TryParseExact. Read the documentation: DateTime.TryParseExact Method (String, String, IFormatProvider, DateTimeStyles, DateTime) (System)[^]. The format string for 'YYYYMMDD' will be "yyyyMMdd". The provider should be CultureInfo.InvariantCulture. The style should be DateTimeStyles.None. So use DateTime dateValue; bool isValidDate = DateTime.TryParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue);
您应该使用其中一种DateTime方法,例如DateTime.Compare方法(日期时间,日期时间)(系统) [ ^ ]。 You should use one of the DateTime methods, such as DateTime.Compare Method (DateTime, DateTime) (System)[^].
string d = "20170413"; // using ParseExact DateTime dt1 = DateTime.ParseExact(d, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); // using TryParseExact DateTime dt2; if (DateTime.TryParseExact(d, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dt2)) { // the conversion worked } else { // the conversion failed }
更多推荐
如何使用tryparseexact来比较datetime
发布评论