Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI Guys

I am currently experiencing some trouble on my production box.

On my dev and UAT i have not had this issue but i do have this issue on the production server

I am running a import file service which collects files from an FTP location and imports into the db.

When i import the data i am getting an error
Conversion from string "1/25/2017 7:59:26 AM" to type 'Date' is not valid

I am not too sure what the issue could be

What I have tried:

Currently i am reading the data from an array and then using the following code to convert the date to a standard format
Dim importeddate as datetime
ImportedDate = CDate(fields(Count))
Dim FormattedDate As String = ImportedDate.ToString("dd/MM/yyyy HH:mm:ss")

I have also tried using datetime.parse(fields(count).tostring)
Posted
Updated 26-Jan-17 16:54pm
v2

There are two things here. Firstly, when reading a date, the system will use your system's local formats to interpret it. I notice the date being imported is in the US format (M/dd/yyyy), where you are outputting in the British format (dd/MM/yyyy). If your system uses the British format, then it will fail when reading any US date where the day is greater than 12, and will get it wrong in all other cases.

The 2nd thing is that you should probably use the DataTime.TryParse method to parse the date. This will return true/false based on success or failure, rather than throwing an exception.

As such, I would be looking at using the DateTime.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTime) (System)[^], that allows you to specify the culture that the string format of the date comes from, and does not throw an exception on failure.
 
Share this answer
 
Comments
IsiR_19 26-Jan-17 19:06pm    
See the issue i have is users import multiple date types

So it could be the british date or the US or the could be yyyy/mm/dd HH:mm:ss

That was why i initially added the Cdate in,on my local machine i dont have this error being thrown if i Cdate the date in the string it does not have any issues
Midi_Mick 26-Jan-17 19:24pm    
You need to know which format is being imported. Otherwise, is 2/3/2017 the 2nd of March, or the 3rd of February? And if you CDate "1/25/2017" parsing with the British format defined, you will get an error, as there is no 25th month.

You might want to look at the TryParseExact method (https://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact(v=vs.110).aspx) if you can try to summise what the date structure is going to be.
Note: If you parse without specifying a format, it will use whatever format is defined by your operating system - so you may get different results on different machines.
If you have control over the input of the datetime data, you can use CultureInfo.InvariantCulture Property (System.Globalization)[^] in the conversion.
Learn more from Avoid common globalization errors in .NET[^].
If not, you have to know the format of the datetime data that you are receiving before any meaningful parsing. Otherwise, no amount of parsing, even if it passes, can promise you that it is the correct datetime that you think you are getting.
 
Share this answer
 
v5

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