Invoke Code to convert date format MM/dd/yy h:mm tt to yy-MM-dd hh:mm:ss

Afternoon All,

Hoping someone can help me with a date format question. I’m using the invoke code to convert the dates from an excel sheet prior to writing out a delimited file. I’m getting an error on the code.

Input date example: 1/29/21 12:00 AM
I want to output the following format : 2021-01-29 0:00:00

Using Invoke Code - CleanTable.AsEnumerable().ToList().ForEach(Sub(row)row(“Effective Date”)=DateTime.ParseExact(row(“Effective Date”).ToString,“MM/dd/yy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture).ToString(“yy-MM-dd hh:mm:ss”))

Get error :
Invoke code to convert effective date : Exception has been thrown by the target of an invocation.

Any help would be appreciated.

@jsilvestre - Could you please share the sample data?

Is row(“Effective Date”) a datetime? If so you just need to do row(“Effective Date”).ToString(“yyyy-MM-dd hh:mm:ss”)


Input date example: 1/29/21 12:00 AM
I want to output the following format : 2021-01-29 0:00:00

The parse should be:
DateTime.ParseExact(row(“Effective Date”).ToString,“%M/dd/yy h:mm:ss tt”,System.Globalization.CultureInfo.InvariantCulture).ToString(“yy-MM-dd hh:mm:ss”))

Since your input date has 1 in month, instead of 01

If row(“Effective Date”) isn’t a datetime, then…

Date.Parse(row(“Effective Date”).ToString).ToString(“yyyy-MM-dd hh:mm:ss”)

SampleData.xlsx (10.1 KB)

The posted and effective dates are the columns I’m trying to convert.

You have the columns formatted as Date, so you need to just change the column format to what you want.

@jorgejogyt,Tried the example but I received the same error during the invoke code.

The columns are formatted as Date in the spreadsheet, they’re not text. So you have to change the format in the spreadsheet to what you want.


I’m using the excel application scope with the read range to pull in the table. Once inside I want to convert the dates prior to writing a pipe delimited file. The bot will only have read access to the source.

Thank you

Is the column in the DT a datetime or string? Either way you’re overcomplicating things using Invoke Code. Just do a For Each Row with assigns to change the format (assuming the columns are strings). Or use the For Each to build the pipe delimited data into one long string, converting the dates as you go, and then write that to the file.

it’s a date

I’m asking about the datatype of the column in the DT. Is it datetime or string?

@jsilvestre -

without using Preserve format, your data will print as OADate

With Preserver Format, it will print as below…

So, from the OADate we have to convert the it to the required format. I am looking at this…

Sorry, I’m using the Read Range from the excel application with the AddHeaders and PreserveFormat options on. I’m not sure but I think that’s a string.

So just do a For Each Row to build the pipe delimited string, converting the dates on the fly.

@jsilvestre - is this your expected output??

yes, that’s the expected result. Do I turn of the preserve format option?


But hold on…If you don’t use preserve format…other column values will get changed while writing the output…let me think…

@prasath17 , Thanks to your help with writing out the data using the Preserve format option, I figured out my issue. I was able to use my original code just updating the format

CleanTable.AsEnumerable().ToList().ForEach(Sub(row)row(“Effective Date”)=DateTime.ParseExact(row(“Effective Date”).ToString,“MM/dd/yyyy 00:mm:ss”,System.Globalization.CultureInfo.InvariantCulture).ToString(“yyyy-MM-dd 00:mm:ss”))