简体   繁体   English

C# DateTime.ParseExact 给出“字符串未被识别为有效的日期时间值。”

[英]C# DateTime.ParseExact gives "The string was not recognized as valid DateTime-value."

I try to debug and it says "String is/was not recognized as valid DateTime-value", I found about 20 posts about this at this website and tried every one of th ose, none made any difference or it gave extra error "0 is not valid value".我尝试调试,它说“字符串是/未被识别为有效的日期时间值”,我在这个网站上找到了大约 20 篇关于这个的帖子,并尝试了其中的每一个,没有任何区别,或者它给出了额外的错误“0不是有效值”。 What am I doing wrong?我究竟做错了什么? Because it does not show any errors but stops debugging every time and gives the error.因为它没有显示任何错误,而是每次都停止调试并给出错误。

dToday2 and cExpiration I want to convert from a string to DateTime value and then compare them in the bottom line of code. dToday2 和 cExpiration 我想从字符串转换为 DateTime 值,然后在代码的底部比较它们。

Note, the cExpiration comes from a textBox9.text which is straight readAllText stream from a saved file, so I want it to parse the saved file to DateTime.请注意,cExpiration 来自 textBox9.text,它是来自保存文件的直接 readAllText 流,因此我希望它将保存的文件解析为 DateTime。

            string cheatExpiration = System.IO.File.ReadAllText(@"C:\xWQcixf07xES5yf5V5A6\UKI9nRuJgZA611zQCyIq.txt");
            DateTime dateToday = DateTime.Today;
            string dateToday2 = DateTime.Today.ToString();
            textBox8.Text = dateToday2;

            textBox9.Text = cheatExpiration;
            DateTime cExpiration = DateTime.ParseExact(textBox9.Text, "yyyy/MM/dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            DateTime dToday2 = DateTime.ParseExact(dateToday2, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture);

            if (dToday2 < cExpiration)
            {

            }

I expect it to parse textBox9.text as for example 2019/01/17 01:01 but I don't know because I have not even had a working experience with it so I don't know for sure.我希望它解析 textBox9.text 例如 2019/01/17 01:01 但我不知道,因为我什至没有使用它的工作经验,所以我不确定。

Update更新

在此处输入图片说明

Your problem is exactly as we thought, your date time isn't in the format "yyyy/MM/dd HH:mm" , it IS "yyyy/MM/dd HH:mm\\r\\n"你的问题和我们想的一样,你的日期时间不是格式"yyyy/MM/dd HH:mm" ,它"yyyy/MM/dd HH:mm\\r\\n"

So how do we fix it?那么我们该如何修复呢? We can use the String.Trim Method, which will remove any white space or carriage returns form the string我们可以使用String.Trim方法,该方法将从字符串中删除任何空格或回车

DateTime cExpiration = DateTime.ParseExact(cheatExpiration.Trim(), "yyyy/MM/dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);

You need to narrow down your problem between input that works and input that does not.您需要缩小有效输入和无效输入之间的问题。 As an example, here is input that works fine:例如,这里是工作正常的输入:

var dt = DateTime.ParseExact("2019/01/17 01:01", "yyyy/MM/dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine("{0}", dt);

Starting with this, add more of your own code until it breaks and then narrow down exactly what change caused it to break.从这个开始,添加更多你自己的代码直到它中断,然后准确缩小导致它中断的变化。

You're opening yourself up to other parse errors by converting today's date to a string and then attempting to convert it back to a DateTime again.通过将今天的日期转换为字符串,然后尝试再次将其转换回 DateTime,您将面临其他解析错误。 This is because calling ToString() will result in different outputs in different regions and/or cultures.这是因为调用 ToString() 将导致不同地区和/或文化的不同输出。 Instead, simply compare DateTime.Today directly:相反,只需直接比较 DateTime.Today:

// this includes the Trim() suggested by the others
string cheatExpiration = System.IO.File.ReadAllText(@"C:\xWQcixf07xES5yf5V5A6\UKI9nRuJgZA611zQCyIq.txt").Trim();
DateTime cExpiration = DateTime.ParseExact(cheatExpiration, "yyyy/MM/dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
if (DateTime.Today < cExpiration)
{

}

暂无
暂无

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

相关问题 使用DateTime.ParseExact C#无法将字符串识别为有效的DateTime - String was not recognized as a valid DateTime using DateTime.ParseExact C# DateTime.ParseExact赋予String未被识别为有效的DateTime。 - DateTime.ParseExact gives String was not recognized as a valid DateTime. 在DateTime.ParseExact上未将字符串识别为有效的DateTime - String was not recognized as a valid DateTime on DateTime.ParseExact DateTime.ParseExact字符串未被识别为有效的日期时间 - DateTime.ParseExact String not recognized as valid datetime DateTime.ParseExact:字符串未被识别为有效的DateTime - DateTime.ParseExact: String was not recognized as a valid DateTime DateTime.ParseExact()-无法将字符串识别为有效的DateTime - DateTime.ParseExact() - String was not recognized as a valid DateTime 无法将字符串识别为DateTime.ParseExact的有效参数 - String was not recognized as a valid parameter for DateTime.ParseExact 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# DateTime.ParseExact FormatException字符串未被识别为有效的DateTime - DateTime.ParseExact FormatException String was not recognized as a valid DateTime DateTime.ParseExact给出错误:字符串未被识别为有效的DateTime - DateTime.ParseExact give the error: String was not recognized as a valid DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM