Convert datetime

Hi,
I need to convert the string “01-11-2023” into date time format(dd-MM-yyyy) please help i tried to do this but it is showing error

Hi @alan_prakash

StringDate = “01-11-2023”
DateTimeVariable = DateTime.ParseExact(StringDate, “dd-MM-yyyy”, System.Globalization.CultureInfo.InvariantCulture)

Cheers!!

Datetimes don’t have a format. They store a value. You format values when you output them as a string. “01-11-2023” is already “dd-MM-yyyy” so what are you trying to do?

I have tried this but it is showing the error Assign: Expression Activity type ‘VisualBasicValue`1’ requires compilation in order to run. Please ensure that the workflow has been compiled.

I just need to convert to date time that it with the same format

when a string is “01-11-2023” and is needed as dd-MM-yyyy (we assume 11=Nov)
nothing is to do

when string is “01-11-2023” and a Datetime Variable is needed we can parse it

DateTime.ParseExact(YourStringVar, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)

Ensure the correct doublequotes of " and not the inverted ones e.g. by a copy& paste

But as also mentioned by Paul. We do not touch the internal structure of a date time

When working with the DateTime Var (e.g. AddDays(20) ) and we do need the result within another format then we catch the formated string by:
myDateTimeVar.ToString("dd-MM-yyyy")

@alan_prakash

Retype double quotes from the expression

Note:DateTimeVariable datatype is System.DateTime

As I mentioned, datetimes don’t have a format they store a value. You can output a datetime to a string in whatever format you want. To convert a mm-DD-yyyy string to a datetime, use the expression Irtetela mentioned.

Assign: Expression Activity type ‘VisualBasicValue`1’

This is because you copy/pasted the expression from the forum, but the double quotes are an invalid character. Type the expression yourself into your automation.

Now no error is showing but it showing the output as [11/01/2023 00:00:00] this only i need the ouput same as 01-11-2023

Actually i need to compare this date with one of the excel coloumn

we would do it at DateTime base
Parsing Option was given to you e.g. here:

When also Parsing the other Column Value you can use both within the Condition

Also have a look here as Excel visual presented values are not mandatory the value within a datatable:

:ambulance: :sos: [FirstAid] Datatable: Debug & Analysis invalid DateTime Strings / String to DateTime Parsing Issues - News / Tutorials - UiPath Community Forum

I have given this but it showing output as 11/01/2023 00:00:00 i need 01-11-2023

we recommend to reread all given answers again as we elaborated on it, the technical details and also hints for parsing and compare conditions.

Also you can do RnD and experiments within the immediate panel, presented here:

Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

DOCU

Hi @alan_prakash

strDate = “01-11-2023”

dt = DateTime.ParseExact(strDate, “dd-MM-yyyy”, CultureInfo.InvariantCulture)
// dt now contains the DateTime object

Hope this helps