简体   繁体   English

DateTime.TryParseExact 并解析多种日期格式

[英]DateTime.TryParseExact and parsing multiple date formats

I am trying to see if a string is a valid date.我正在尝试查看字符串是否为有效日期。 I am having trouble since the dates are in "2011–7–17 23:18:52" format.由于日期采用“2011–7–17 23:18:52”格式,因此我遇到了麻烦。 I am testing like so:我正在这样测试:

String lstrTime = "2011–7–17 23:18:52";
DateTime datevalue; 
CultureInfo enUS = new CultureInfo("en-US");
if (DateTime.TryParseExact(lstrTime, "yyyy-MM-dd hh:mm:ss", enUS,
                     DateTimeStyles.None, out datevalue))
                        {
                            rtxtOutput.Text += "Valid datetime: " + lstrTime;
                        }

I think the reason I am having trouble is because the date can have only 1 M value or 2 MM for months.我认为我遇到麻烦的原因是因为日期只能有 1 M 值或 2 MM 几个月。 I have bulk data to process, and changing out the M to MM wont happen.我有大量数据要处理,将 M 更改为 MM 不会发生。

Edit: I have tried all suggested link, perhaps it could be the cultureinfo not recognizing a 24 hour clock(questionmark)编辑:我已经尝试了所有建议的链接,也许它可能是文化信息无法识别 24 小时制(问号)

你需要使用24小时的时间,即大写字母H,“yyyy-MM-dd HH:mm:ss”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM