Data table Date format

Hi,
My data table date has this format. mm/dd/yyyy hh:mm
For example “8/8/2019 17:00” and “12/4/2019 8:00”

I am trying to convert it to “ddd MM-dd-yy” format for example “Thu 08-10-19”

The following Invoke code is not working. Can you please help? Thank you,

dt.AsEnumerable().ToList().ForEach(Sub(row) row(“ExampeDate”) = Convert.ToDateTime(row(“ExampleDate”).ToString.trim()).ToString(“ddd MM-dd-yy”))

Hi @A_Learner ,

You might have to take a look at how the values are stored in the bot, and whether there are any null values before trying the conversion.

For now, could you give this a try?

dt.AsEnumerable().ToList().ForEach(Sub(row) row(“ExampeDate”) = DateTime.ParseExact(row(“ExampleDate”).ToString.trim,"MM/dd/yyyy hh:mm",Nothing).ToString(“dd-MMM-yyyy”))

Kind Regards,
Ashwin A.K

Thank you @ashwin.ashok

Getting the following exception

System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at UiPathCodeRunner_c10fe1255659478caab7646a54888287._Closure$__._Lambda$__0-0(DataRow row)
at System.Collections.Generic.List1.ForEach(Action1 action)

If you want it in Date Type Format , Remove .ToString(“dd-MMM-yyyy”)

HI @A_Learner

Check the format by giving it in message box and after that give that format in the @ashwin.ashok 's expression

that is
dt.AsEnumerable().ToList().ForEach(Sub(row) row(“ExampeDate”) = DateTime.ParseExact(row(“ExampleDate”).ToString.trim,“Check your format and pass the format here”,Nothing).ToString(“dd-MMM-yyyy”))

If you have multiple formats in the datatable

dt.AsEnumerable().ToList().ForEach(Sub(row) row(“ExampeDate”) = DateTime.ParseExact(row(“ExampleDate”).ToString.trim,arrFormat,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString(“dd-MMM-yyyy”))

where arrFormat is array of string where you will be giving all the possible formats
Eg: {“MM/dd/yyyy hh:mm”,“dd.MM.yyyy”}

This is not the format of your date “8/8/2019 17:00”

it will be “M/d/yyyy HH:mm

So try with this

dt.AsEnumerable().ToList().ForEach(Sub(row) row(“ExampeDate”) = DateTime.ParseExact(row(“ExampleDate”).ToString.trim,"M/d/yyyy HH:mm",Nothing).ToString(“ddd MM-dd-yy”))

Regards
Sudharsan