Change column type to datetime

Hello All!

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

1 Like

@Betul_Dundar

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

Import System.Globalisation

Hi @Betul_Dundar

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

Regards

Test1.xlsx (8.5 KB)
This is my excel

Not working. Can u please check my excel file?

@Betul_Dundar

Here is the updated 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
	dt=dt
Next

This will convert the format into Custom.

Regards

@Betul_Dundar

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

Hi @Betul_Dundar

Can you try the below

image

Cheers!!

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.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.