Question Issue with Convert.ToDateTime

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
I'm using VB express 2010 and have been working on reading a text file into an MSSQL database. Once I read the file into the database I convert a date/time string into a new date/time format and add 15 minutes to that time. Keep in mind, every line coming into the database is currently in EST. I'm trying to take the file in and convert it to CST and then add 15 minutes.

This seams to work fine for me here in the US, but I've found that people in some other countries (GTB Standard Time, for example) will get an exception error:
String was not recognized as a valid DateTime.

Here is some of my code...
VB.NET:
'convert date time string to date time format and add 15 minutes
                ConvertedTime = Convert.ToDateTime(strArray(2))
                newConvertedTime = ConvertedTime.AddMinutes(addtime)
                strArray(2) = newConvertedTime
                'subtract one hour to make Central time
                newConvertedTime = newConvertedTime.AddHours(-1)
                Format(newConvertedTime, "hh:mm AM/PM")
                'Format(newConvertedTime.ToShortTimeString)
                strArray(2) = newConvertedTime
                strArray(0) = strArray(0).PadRight(78, " ")
                strArray(2) = strArray(2).PadRight(12, " ")
                'Add the following two lines of code so seconds don't appear in Gridview
                strArray(2) = strArray(2).Replace(":00 AM", " AM")
                strArray(2) = strArray(2).Replace(":00 PM", " PM")
                DomainData(cnt) = strArray(0) & strArray(2)

From reading the complete exception error it appears the main issue is around line 2.

Any help would be appriciated!
 
If your text data is in a specific format then you should be specifying that format. If you don't specify a format then it will just use the format defined by the system, which can obviously differ from system to system. You could be using the ToDateTime overload that takes an IFormatProvider for the appropriate culture or you could use Date.ParseExact and specify the exact input format.

Also, unless you can 100% guarantee that the input data will be valid, I'd suggest using Date.TryParse or Date.TryParseExact, which will handle invalid data without throwing an exception.

Finally, you might want to consider something like this for the output:
Private Function DateToString(dt As Date) As String
    Return String.Format("{0:hh}{1} {0:tt}",
                         dt,
                         If(dt.Minute = 0,
                            String.Empty,
                            dt.ToString(":mm")))
End Function
Sample usage:
Dim dt1 = #11:00:00 AM#
Dim dt2 = #11:45:00 AM#
Dim dt3 = #12:00:00 PM#
Dim dt4 = #12:15:00 PM#

Console.WriteLine(DateToString(dt1))
Console.WriteLine(DateToString(dt2))
Console.WriteLine(DateToString(dt3))
Console.WriteLine(DateToString(dt4))
 
Hey jmcil - thanks for your reply. I do think the issue is because I'm not specifying the format, although I'm not exactly sure where I need to be specifying that at.

I considered using Date.TryParse before but this data is also going to be valid and it's always going to be the exact same.

I'll try out the examples you posted, thanks for that. Any suggestions on specifying the format would be appriciated!
 
This is where the conversion from String to Date is taking place:
ConvertedTime = Convert.ToDateTime(strArray(2))
so that's where you need to specify the format. If all the data is in one format then using Date.ParseExact is probably your best option, e.g.
ConvertedTime = Date.ParseExact(strArray(2), "M/dd/yyyy HH:mm:ss", Nothing)
You can hard-code the appropriate format or you could perhaps store it in the config file and read it from there, allowing you to change it later without recompiling.
 
Thanks for your reply jmcilhinney..I've tried with your code but get an unhandled exception:
String was not recognized as a valid DateTime.

I guess the data isn't in one exact format so I'll have to double check on that..Hardcoding into the software sounds like the idea route. I would prefer the software to always use CST as it does have some other features that use date/time stuff.
 
The ParseExact method does allow you to specify more than one possible input format. You really should start reading the documentation because it's quite easy to find this stuff for yourself. The Help menu is not provided just for decoration.
 
Back
Top