Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am importing data from a supplier's database. As my supplier is foreign, the data is arriving with their nation's date format. Also, as I am importing from an external supplier, I want to validate all the data they supply me. I was trying to use convert.todatetime(string) but am getting the expected "FormatException was Unhandled" error "String was not recongnized as a valid DateTime". I'm really stuck and do not know enough C# to know how to move beyond this problem. Can anyone please point me in the right direction,
cheers,
Stu


Using the information provided, (and thanks to those who have contributed) I have now expanded my code to:
string myTime = DateTime.Parse(pRow[26].ToString()).ToString(CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern); 


The value of pRow[26] is "7/30/2008" but I'm still getting

"FormatException was Unhandled
String was not recognized as a valid DateTime."

And looking over this again, I'm wondering what I'm going to get once I get past the errors. Once I've happily imported the foreign date format, how do I then store it as my local date? I'm now so confused I want to go and work on something else, but I don't want to leave the question open and ignored.

And going one step further, if I'm looping through a datatable of dates, running the TryParse option, how will the computer know if "3/5/2008" should be 3rd May 2008 or 5th March 2008?
Posted
Updated 21-Jun-12 21:45pm
v3
Comments
Stu Baby 22-Jun-12 2:08am    
I've tried solution 3 with:

string myTime = DateTime.Parse(pRow[26].ToString()).ToString(CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern);
and I'm still getting
"FormatException was Unhandled" error "String was not recongnized as a valid DateTime". The value of pRow[26] is "7/3-/2008"

.NET provides a lot of conversion methods for DateTime, Convert.ToDateTime is the simplest. How it works is to wrap the DateTime.TryParse method which provides finer control. Having said that, the first stage is to try giving it a Culture, if the data is in a specific cultural format: Convert.ToString has an overload for that: http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx[^]

If that doesn't help, and the data is always in a specific format, then DateTime.TryParseExact provides culture and format specific methods which should be able to decipher the date info: http://msdn.microsoft.com/en-us/library/ms131044[^] and http://msdn.microsoft.com/en-us/library/h9b85w22[^] - the later of which allows you to specific a range of conversion formats to try.
 
Share this answer
 
Comments
Maciej Los 22-Jun-12 4:59am    
Good answer, my 5!
And going one step further, if I'm looping through a datatable of dates, running the TryParse option, how will the computer know if "3/5/2008" should be 3rd May 2008 or 5th March 2008?

Once you got the data, you will store it in an appropriate data type. DateTime[^] will do in your software, some similar data type should exist in every DBMS. Or, at least, you formatted the data. So you will know what it means.

To get there, provide a culture for every supplier.
C#
bool success = DateTime.TryParse(
    suppliedDateString,
    new System.Globalization.CultureInfo("fr-FR"),
    System.Globalization.DateTimeStyles.None,
    out suppliedDate
);
You can get the culture identification strings ("fr-FR") from here[^].

Or use DateTime.TryParseExact()[^] with different formatting for every supplier.
 
Share this answer
 
v4
Comments
Stu Baby 22-Jun-12 3:57am    
Thanks Lukeer, but this is either missing my point, or I'm not making myself clear, to which I apologise. The data is coming from an external source. I know the nationality of where that source is from, but how do I tell the computer that using TryParse?
lukeer 22-Jun-12 4:07am    
I updated my answer.
Stu Baby 22-Jun-12 4:18am    
Thanks Lukeer, but this creates the error:
No overload for method 'TryParse' takes '3' arguments.
lukeer 22-Jun-12 4:43am    
Sorry, forgot the DateTimeStyles.None parameter.
Stu Baby 22-Jun-12 4:36am    
Thanks Lukeer, your comments and suggestions led me to now fully understand DateTime.tryParseExact which I think is the only way I was going to be able to solve this. My final answer:
<pre>
bool succes = DateTime.TryParseExact(pRow[26].ToString(), "d", new System.Globalization.CultureInfo("en-US"), DateTimeStyles.None , out suppliedDate);
</pre>
 
Share this answer
 
Comments
Maciej Los 22-Jun-12 5:00am    
Good links, my 5!
Try datetime.parse() MSDN[^] if that does not work then isolate the formats that fail and I'd bet they are actually invalid dates! You can also expand your question with the fail examples!
 
Share this answer
 
Comments
Stu Baby 22-Jun-12 1:55am    
OK, but is there a way I can insert the supplied format? The logical expansion of this then being a way to allow the method to handle any supplier's dates. EG Supplier#1 is French, Supplier #2 is US. The dates themselves DO appear to all be correct, the problem I'm trying to solve is how to tell the computer WHY they are correct. Or am I missing something and this will happen or am I not explaining myself properly.
Mycroft Holmes 22-Jun-12 2:07am    
See Griff's solution it is more detailed. If it fails the TryParseExact with the culture supplied then it is crappy data and you need to deal with it. Mapping the cultureinfo to a supplier is just work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900