I want to change my column type to datetime format.
I have two columns names are: Date1 , Date2
Format like : “2/17/2023 15:30”
How can i change to datetime format?
I used this invoke code but it doesnt work.
For Each row As DataRow In dt.Rows
row("Date1") = DateTime.ParseExact(row("Date1").ToString(), "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
row("Date2") = DateTime.ParseExact(row("Date2").ToString(), "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
Next
For Each row As DataRow In dt.Rows
If Not String.IsNullOrWhiteSpace(row("Date1").ToString()) Then
row("Date1") = DateTime.ParseExact(row("Date1").ToString(), "M/dd/yyyy H:mm", System.Globalization.CultureInfo.InvariantCulture)
End If
If Not String.IsNullOrWhiteSpace(row("Date2").ToString()) Then
row("Date2") = DateTime.ParseExact(row("Date2").ToString(), "M/dd/yyyy H:mm", System.Globalization.CultureInfo.InvariantCulture)
End If
Next
Could you share the input file. In the mean while you can try the below code:
For Each row As DataRow In dt.Rows
Try
If Not String.IsNullOrEmpty(row("Date1").ToString()) Then
row("Date1") = DateTime.ParseExact(row("Date1").ToString(), "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
End If
Catch ex As Exception
' Handle the parsing error, log the exception, or take appropriate action
End Try
Try
If Not String.IsNullOrEmpty(row("Date2").ToString()) Then
row("Date2") = DateTime.ParseExact(row("Date2").ToString(), "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
End If
Catch ex As Exception
' Handle the parsing error, log the exception, or take appropriate action
End Try
Next
For Each row As DataRow In dt.Rows
Try
If Not String.IsNullOrEmpty(row("Date1").ToString()) Then
row("Date1") = DateTime.ParseExact(row("Date1").ToString(), "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
End If
Catch ex As Exception
' Handle the parsing error, log the exception, or take appropriate action
End Try
Try
If Not String.IsNullOrEmpty(row("Date2").ToString()) Then
row("Date2") = DateTime.ParseExact(row("Date2").ToString(), "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
End If
Catch ex As Exception
' Handle the parsing error, log the exception, or take appropriate action
End Try
dt=dt
Next
For Each row As DataRow In dt.Rows
If Not String.IsNullOrWhiteSpace(row("Date1").ToString()) Then
row("Date1") = DateTime.ParseExact(row("Date1").ToString(), "M/d/yyyy H:mm", CultureInfo.InvariantCulture)
End If
If Not String.IsNullOrWhiteSpace(row("Date2").ToString()) Then
row("Date2") = DateTime.ParseExact(row("Date2").ToString(), "M/d/yyyy H:mm", CultureInfo.InvariantCulture)
End If
Next
I would advice against Invoke Code.
Coded workflows are intended to replace Invoke Code, you also do not need invoke code here, the coding is pretty straightforward and can be done with the standard loop and assign activities.