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