How to fix date conversion error?

Hello.

I am facing an error while converting date formats to a preferable date format for my process.

The date format of the variable I receive is dd/MM. I get the date I want to convert from a data table named “out_dt_excel” and I store it to the variable “CurrentDate”. In the next assign activity I do this in order to convert the date format to “dd/MM/yyyy”.

out_dt_excel(IndexRow).Item(“Date”)=DateTime.ParseExact(CurrentDate,{“%d %M %yy”,“%d %M”}, System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString(“dd/MM/yyyy”)

I have it before also with the same format (“dd/MM”) and it worked, but now suddenly this error arise and I don’t know what I should change in order to work.
Error: Change format of current date: String ‘29 02’ was not recognized as a valid DateTime.

I tried also in the immediate CDate(CurrentDate) but I also get an exception.

What should I do? Do you have any recommendations?

Thank you in advance.

Hi @aikaterini.karakasidi

Issue is29/02 fails because no year is provided, and leap year validation is required.

Try this,

CurrentDate = CurrentDate & “/” & Now.Year
ParsedDate = DateTime.ParseExact(CurrentDate, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

Leap Year Handling:

year = Now.Year
If Not DateTime.IsLeapYear(year) AndAlso CurrentDate.StartsWith(“29/02”) Then year = 2024
CurrentDate = CurrentDate & “/” & year
ParsedDate = DateTime.ParseExact(CurrentDate, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

2 Likes

It appears the code is trying to convert “29/02” into “29/02/2025,” which results in an invalid date.

What do you need in such cases? Would using 2024 as the year work for you?

1 Like

You are both right. I didn’t check that the code was applying the dates of the current year and that’s why I had this issue.

I solved it by replacing indeed 2024.

Thank you for your time and comments.

1 Like

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