Convert strings to DateTime

I am trying to convert a string from a text file to DateTime, but I am getting strange results. It works on one computer, but not on another. How can I do this work on all computers? In my last question, you said add culture, and it worked for several minutes, but now it does not work again. Here is 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 get an error String was not recognized as a valid DateTime.

Any clues? Thanks!

Edit: using MessageBox.Show(stringArray[(i * 5) + 4]);i get26/10/2013 11:58:03 AM

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

DateTime Today = DateTime.Now;
            Today = DateTime.ParseExact(Today.ToString(), "dd/MM/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
+4
source share
2 answers

: 26/10/2013 11:58:03 AM. //, //.

, , . - , , .


: ?

, dd/MM/yyyy . Today.ToString() , MM/dd/yyyy, .

. , : theDate.ToString(CultureInfo.InvariantCulture), Convert.ToDateTime(theString, CultureInfo.InvariantCulture), , ( ) , .

+1

"dd/MM/yyyy HH:mm:ss t" InvariantCulture, :

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);

:

10/26/2013 11:58:03 AM

demonstration.

:

EDIT. DateTime.Now "dd/MM/yyyy HH:mm:ss tt", . InvariantCulture DateTime.Now.

0

All Articles