[英]DateTime.TryParseExact failing with seemingly correct format C#
I have the following code in a test project that I'm working on. 我正在处理的测试项目中有以下代码。 Trouble is,
TryParseExact
keeps displaying an error instead of parsing my date correctly and storing in the dParsedDate
variable. 麻烦的是,
TryParseExact
一直显示错误,而不是正确解析我的日期并存储在dParsedDate
变量中。 What am I doing wrong here? 我在这里做错了什么? I can't see any issues with my format pattern so I'm at a loss here.
我的格式模式看不到任何问题,所以我很茫然。
string sDateTime = "2015-09-15 09:09:09";
string sPattern = "YYYY-MM-dd HH:mm:ss";
DateTime dParsedDate;
if (!DateTime.TryParseExact(sDateTime, sPattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dParsedDate))
textBox2.Text = "ERROR";
else
textBox2.Text = dParsedDate.ToString();
There is no YYYY
format string for DateTime
. DateTime
没有YYYY
格式的字符串 。 Use lowercase y
's. 使用小写的
y
。
string sPattern = "yyyy-MM-dd HH:mm:ss";
As per msdn documentation at DateTime.TryParseExact 根据DateTime.TryParseExact上的 msdn文档
DateTime dParsedDate;
if (!DateTime.TryParseExact(sDateTime, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dParsedDate))
textBox2.Text = "ERROR";
else
textBox2.Text = dParsedDate.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.