由网友(痛不欲生的念)分享简介:由于字符串15时30分20秒和2011-09-02 15时30分20秒,如何动态检查,如果给定的字符串包含日期或不是?15时30分二十秒 - >无效2011-09-02 15时30分20秒=>有效解决方案 使用DateTime.TryParseExact方法。的String []格式=新的String [...![字符串和日期时间的处理](https://p.xsw88.cn/allimgs/daicuo/20230903/1419.png)
由于字符串15时30分20秒
和2011-09-02 15时30分20秒
,
如何动态检查,如果给定的字符串包含日期或不是?
15时30分二十秒 - >无效
2011-09-02 15时30分20秒=>有效
解决方案
使用DateTime.TryParseExact方法。
的String []格式=新的String [] {YYYY-MM-DD HH:MM:SS};
字符串值=2011-09-02 15点30分二十秒;
日期时间日期时间;
如果(DateTime.TryParseExact(价值,格式, System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.NoCurrentDateDefault ,出日期时间))
Console.WriteLine(有效+日期时间);
其他
Console.WriteLine(无效);
![字符串和日期时间的处理](https://p.xsw88.cn/allimgs/daicuo/20230903/1419.png)
Given a string "15:30:20"
and "2011-09-02 15:30:20"
,
How can I dynamically check if a given string contains date or not?
"15:30:20" -> Not Valid
"2011-09-02 15:30:20" => Valid
解决方案
Use DateTime.TryParseExact Method.
string []format = new string []{"yyyy-MM-dd HH:mm:ss"};
string value = "2011-09-02 15:30:20";
DateTime datetime;
if (DateTime.TryParseExact(value, format, System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.NoCurrentDateDefault , out datetime))
Console.WriteLine("Valid : " + datetime);
else
Console.WriteLine("Invalid");
相关推荐
最新文章