How to convert string with datetime format "dd-MMM-yyyy" to "d/M/yyyy"

I tried to convert string to datetime with format “dd-MMM-yyyy” before converting back to string with format “d/M/yyyy” but an error would appear saying cannot assign to my new variable name which is in string datatype

DateTime.ParseExact(StartBlackout,“dd-MMM-yyyy”,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString(“d/M/yyyy”) is the code

I also tried splitting by getting the DateTime variable first but it also comes out with the same cannot assign error. Any part I might have had an oversight? Thanks

Hello

Have you tried?
CDate(StartBlackout)

If that fails, take a look at this xaml.

Utility_ChangeDate_Format.xaml (12.6 KB)

Update the arguments as follows:

Cheers
Steve

Hi @hellos

Are you getting timestamp in your input variable? If yes then try the below syntax:

DateTime.ParseExact(StartBlackout.ToString.Split(" ").First, "dd-MMM-yyyy", System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("d/M/yyyy")

Hope it helps!!

1 Like

HI,

Can you share content of StartBlackout and expected output?

Regards,

hi,

StartBlackout is a get text output variable (A) always in the form “dd-MMM-yyyy”

and i want to get a new string variable (B) with the form “d/M/yyyy” so that i can retrieve a datetime value from excel and create a condition that is something along the lines of B = exceldate (which is in the form d/M/yyyy)

thanks

Hi,

There might be extra whitespace before or after the date string.
For now, can you try Trim method as the following?

DateTime.ParseExact(StartBlackout.Trim(),"dd-MMM-yyyy",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("d/M/yyyy")

If still have error, can you share your string as file using WriteTextFile activity?

Regards,

1 Like

Hi @hellos

Your expression is perfect there is no need to change the Expression.

DateTime.ParseExact(StartBlackout, "dd-MMM-yyyy", System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("d/M/yyyy")

If possible show the Error what it was showing and print the Get text value you are getting in Immediate panel and share a screenshot with us.

Hope you understand!!

i managed to get excelStart value to be exactly the same format as realStart but when i run the if else function, it always gives me false even tho both are same datatype of string? what could possibly be the error?

thanks

Can you show me the output panel that write line prints… @hellos

hi,

image

i checked for realStart then excelStart, realEnd then excelEnd then the datatypes of realStart and excelStart

they all were in accordance to what i needed but somehow it does not run the if (true) part instead directly runs to the else part

Can you try the below condition in else if activity,

(realStart.equals(excelStart)) andalso (realEnd.equals(excelEnd))

Hope it helps!!

1 Like

damn thanks, do you know what exactly caused that statement above to not run as TRUE?

1 Like


びょう
Use DateTime.ParseExact

Might be You given condition is wrong… @hellos

I hope you find the solution for your query. Make my post Mark as solution to close the loop.

Happy Automation!!

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