简体   繁体   中英

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". 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.

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.

            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.

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"

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

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. This is because calling ToString() will result in different outputs in different regions and/or cultures. Instead, simply compare DateTime.Today directly:

// 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)
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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