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.
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
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.
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.
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.
@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