Convert 01012020 date format to 01-01-2020

Hello All,

I have a datatable DT with column date in the form like 20200101 and I would like to convert it to 01-01-2020 for the column.

I used the below expression in Assign:
row(“Reception Date”)= Datetime.ParseExact(row(“Reception date”).ToString,“dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture)

I am getting below error:
String was not recognized as a valid DateTime.

Does anyone have a better solution for it?

Thanks,
Yogendra

Use:

DateTime.ParseExact(row(“Reception date”).ToString,“yyyyMMdd”,System.Globalization.CultureInfo.InvariantCulture).ToString("dd-MM-yyyy")

2 Likes

Thanks Adrian this worked for me.
I just have one question can this work with directly datatable variable instead of using for each loop. As the data is very large for each takes lot of time.

1 Like

Do you need to iterate the Date conversion over more cases in the table? If so, for each row is required, but you can, for example, filter the data table to separatre DT leaving only the date column (for increasing speed), and then merge the processed table with the result table.

ok thank you yes I have more than 10000 records.
I will seperate the DT.

Thanks a lot

1 Like

@yogendra.kulkarni - Check this…

DtInputDate.AsEnumerable().OrderByDescending(Function(b) (DateTime.ParseExact(b(“Receiption Date”).ToString,“yyyyMMdd”,System.Globalization.CultureInfo.InvariantCulture).Tostring(“dd-MM-yyyy”))).CopyToDataTable()

I have already tested it…and its working…

2 Likes

Ah, yes Linq… :slight_smile: How fast is this LINQ function?
:slight_smile::slight_smile:
I’m not skillful in linq. :sweat_smile:

@Adrian_Star…not sure about this LINQ… I used in one of my project which had 35K rows and LINq query ran less than 5 mins where as filter datatable taking 15 plus mins.

@yogendra.kulkarni - I have figured out some issue with my LINQ query. Since I used on my existing code, I Initially thought it was working.

Let me fix it.

ok great,
It will be better for me with LINQ as I will have lot of records everytime.

Thanks

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