Search code examples
vb.netstringssisdate-conversion

String was not recognized as a valid date - VB.NET


In my SSIS project I have a date variable of type [DT_DATE]

I'm trying to convert rows which contains a string (a date and a time)

The format of the string looks like this: 20151107 19:32:23 I want to convert this into a datetime format before I insert it into my db.

My script looks like this:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Dim tempDate As String = Row.calldate

        Row.newcalldate = Date.ParseExact(tempDate, "yyyyMMdd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)


    End Sub

I end up with the error: String was not recognized as a valid date

Any help would be much appreciated


Solution

  • In the format string hh stands for the hour using a 12-hour clock from 01 to 12. So if you want to parse a time expressed in 24-hour clock you need to use HH

    Row.newcalldate = Date.ParseExact(tempDate, "yyyyMMdd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)