简体   繁体   中英

String to DateTime conversion Failing

I'm trying to convert a string from a text file to a DateTime, but I'm getting weird results. On one computer, it works, but another, it doesn't. How would I make this work on all computers? On my last question, you said to add the culture thing, and it worked for a few minutes, but it's not working again now. Heres my code:

string[] stringArray = File.ReadAllLines("C:\\Program Files\\PointOfSales\\RecordTotals.txt");
            int lines = stringArray.Length;

            for (int i = 0; i < (lines / 5); i++)
            {
                TransactionList.Add(new Transaction
                {
                    TotalEarned = Convert.ToDouble(stringArray[(i * 5)]),
                    TotalCost = Convert.ToDouble(stringArray[(i * 5) + 1]),
                    TotalHST = Convert.ToDouble(stringArray[(i * 5) + 2]),
                    Category = stringArray[(i * 5) + 3],
                    HoursSince2013 = Convert.ToDateTime(stringArray[(i * 5) + 4], CultureInfo.InvariantCulture)
                });
            }

I'm getting the error String was not recognized as a valid DateTime.

Any clues whats up? Thanks!

Edit: Using MessageBox.Show(stringArray[(i * 5) + 4]); I get 26/10/2013 11:58:03 AM

Edit: Why won't this work to convert current time to the right culture?

DateTime Today = DateTime.Now;
            Today = DateTime.ParseExact(Today.ToString(), "dd/MM/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);

Your string, on that system, is not a valid date in invariant culture: 26/10/2013 11:58:03 AM . The invariant culture expects month/day/year, not day/month/year.

You should specify the same culture that is being used to generate the file that you are reading. You will need to have some way to determine or standardize on the cultures being used for writing to the file, as well as reading from it.


Edit: Why won't this work to convert current time to the right culture?

That will fail if your current culture doesn't use dd/MM/yyyy for it's format. Today.ToString() doesn't have a culture specified, or a format, so it's going to, on a US system, write out using MM/dd/yyyy format, which will cause the call to fail.

In your case, I would recommend always reading and writing using the same culture - If you write your file using: theDate.ToString(CultureInfo.InvariantCulture) , then you can read using Convert.ToDateTime(theString, CultureInfo.InvariantCulture) , and it will work on any system , since the same rules (Invariant culture) are used for both writing and reading.

You should use custom "dd/MM/yyyy HH:mm:ss t" format with InvariantCulture like;

string s = "26/10/2013 11:58:03 AM";
DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine(dt);

Output will be;

10/26/2013 11:58:03 AM

Here a demonstration .

For more information, take a look at;

EDIT : If your DateTime.Now doesn't fit "dd/MM/yyyy HH:mm:ss tt" format, your program will fail. You should use InvariantCulture in all your programs with your DateTime.Now format exactly.

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