如何使用tryparseexact来比较datetime

编程入门 行业动态 更新时间:2024-10-18 19:24:50
本文介绍了如何使用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

本文发布于:2023-11-07 19:43:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   tryparseexact   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!