0

I have a text file with DateTimes stored like this: 30/11/2013 1:18:36 PM

This is fine if you only run the program on one machine, as the DateTime is stored in the same format as the system uses. However I just encountered a problem where if I change user to one of my other accounts, who for some reason is using MM/DD/YYYY format, an error is thrown. How can I read the DateTime regardless of what the system format is? This is what I'm using right now:

RecieptList.Add(new Reciept
                {
                    ...
                    DateNTime = Convert.ToDateTime(stringArray[(i * 12) + 9]), // == 30/11/2013 1:18:36 PM
                    ...
                });

Thanks!

Nathan
  • 1,287
  • 6
  • 15
  • 32
  • I think I should mention what the error was. It failed to convert the string into a datetime. +$exception {"String was not recognized as a valid DateTime."} – Nathan Dec 14 '13 at 16:30
  • Do you have the option to store the data differently? – Joel Rondeau Dec 14 '13 at 16:34
  • What way were you thinking? – Nathan Dec 14 '13 at 16:34
  • 1
    I was thinking that I'd want to know the time zone, and then I'd want to store the datetime as UTC. Not directly part of your question, just a common issue when doing something similar. – Joel Rondeau Dec 14 '13 at 16:37
  • It seems the problem start when you write the date. If you write the date according to the CultureInfo of the current machine and then try to read back in a different culture you are at a dead end. You should store your date in a CultureInfo indipendent way. How do you write these dates? – Steve Dec 14 '13 at 16:38
  • Found a good SO question/answer explaining what I was thinking: http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format – Joel Rondeau Dec 14 '13 at 16:42

2 Answers2

1

you can use TryParseExact() to parse the Dates.

Try This:

String dt = "30/11/2013 1:18:36 PM";//or anydate
DateTime result;
if (DateTime.TryParseExact(dt, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None,out result))
{
   //success use result
}

Edit: as per comments you mentioned that you are storing dates in system dependent culture.

I Strongly suggest you to use CultureInfo.InvarientCulture argument while storing the dates, to store them in Indepedent culture.

so that while reading it in different machines won't produce problems.

Try this: While storing Dates into TextFile

 String dt = DateObject.ToString(CultureInfo.InvariantCulture);

now you can write String dt into TextFile.

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • Sometimes the string is in dd/MM/YYYY, sometimes its MM/dd/yyyy, depeneding on what the system is set to when the date was written. Would your example read a date in MM/dd/YYYY format? – Nathan Dec 14 '13 at 16:34
  • @Nathan so the Date format of the input string varies? that isn't clear from your question. – psubsee2003 Dec 14 '13 at 16:35
  • 2
    @Nathan: Your task is impossible. How to determine if the date format is MM/dd/yyyy or dd/MM/yyyy. Take this as an example: 12/11/2013. Is this the 12th November 2013 or 11th Dezember 2013? – jAC Dec 14 '13 at 16:36
  • @Nathan: my code assumes that your date is in the format of `dd/MM/yyyy` and it wont work if the date is in any other format. – Sudhakar Tillapudi Dec 14 '13 at 16:37
  • Good point Janes, I guess the way I'm doing it now will have to do. Thanks! – Nathan Dec 14 '13 at 16:38
0

You need to parse the date, with the correct settings of the computer. You can get the UI settings by calling Thread.CurrentUICulture .

CultureInfo ci = Thread.CurrentUICulture ; // ci refers to the current UI culture
Convert.ToDateTime(dateStr, ci);

Replace your code with the above , and it should work. You can find more examples below

Convert.ToDateTime Method (String, IFormatProvider)

DateTime.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTime)

Sujay Ghosh
  • 2,828
  • 8
  • 30
  • 47