Datetime saved as 01-01-0001 error

Good day.

I have successfully read an excel file which contains 1 of its columns as a date.
There is no problem with reading and extraction of the values but once I save the value of a specific date to a variable, the value changes to “01-01-0001”

The date it I try to store to a variable is “[02/29/2020 00:00:00]”
image

The Activity I use to store it in a variable is Assign activity with the following code: var_DateIssued = Datetime.ParseExact(CurrentRow.Item(6).tostring, “MM-dd-yyyy hh:mm:ss”, System.Globalization.CultureInfo.InvariantCulture)

But the final value is wrong:

Any help would be greatly appreciated. Thank you!

Hi @jrzcawaling

Try the below syntax:

Datetime.ParseExact(CurrentRow.Item(6).ToString.Replace("[","").Replace("]",""), "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)

Hope it helps!!

Your screenshot is suggesting its already a DateTime variable, so you are converting from a date, to a string, back to a date.

You should be able to assign it directly to your variable by casting it to a DateTime rather than converting to a string then to a DateTime again.

Hi @Parvathy, thanks for the reply.
Unfortunately I’m getting the same result. I believe the square bracket is only there in the input value as it is still a Object type.

Thanks @Jon_Smith for the reply.

I am doing this way as the format from the input is different from the format I need. The format I need is “dd-MM-yyyy”.

DateTime variables don’t have a specific format though. You cannot create a DateTime that has the format "dd-MM-yyyy’ as the DateTime variable to format agnostic.

You can parse strings that are in that format to a date time, but once its parsed it no longer has a fixed format.

It looks like your Excel already read it as a Date Time, so you don’t need to convert to a string or dd-MM-yyyy format to give it to another variable unless you want it to become a string.

Hi @jrzcawaling

Try this,
var_FinalDate = DateTime.ParseExact(CurrentRow.Item(6).ToString, “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).ToString(“dd-MM-yyyy”)

Happy Automation!

Hi @jrzcawaling

Can you share the excel file if possible.

Regards

Hi, The date uses “/” but you used “-”. If you need to keep 24-hour format then use HH instead of hh format.

You should first clean the square brackets from the string and then parse it using the correct format:
var_DateIssued = DateTime.ParseExact(CurrentRow.Item(6).ToString.Trim(“”.ToCharArray), “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture)

Simply try CurrentRow.Item(6).tostring(“dd-MM-yyyy”)

Or whichever format you need

let me know the output so that i can assist better

Hi everyone,

I believe this is a bug on the UiPath side or I’m doing the parsing incorrectly. All dates work correctly except for “02-29-2020,” which converts to “01-01-0001.”

For now I will pass it directly using CDate() as it retains the correct value and will just format it to string whenever I uses it in my automation.

Thanks to @Jon_Smith for the helpful information regarding DateTime – it really helped me in this case.

Thank you all!

Its not a bug, this is core .NET code from Microsoft, its just because you are doing a conversion from Date>string>Date and its going wrong along the way. If you dont convert it and just keep it as a date time you won’t have an issue.