Error in handling Date Time Values

Hello all :slight_smile:

I am automating a process in which I need to use Date Time values. I am having problems because the date time variable is always written in the excel file in format MM/dd/yyyy. For this case I found the solution of using the 2 assign activity.

1st Assign: VarDT = DateTime.Now.ToString(“dd/MM/yyyy”) as string variable.
2nd Assign: VarDT = DateTime.ParseExact(hoy.toString(), “MM/dd/yyyy”, Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

By this way it had been working, but today it appeared this error message: “The string represents an unsupported DateTime in the System.Globalization.GregorianCalendar calendar.”

So, I do not understand why sometimes it works fine just with the first assign activity, and other days I do need to use the 2nd assign activity.

I hope the explanation is easy to understand.

Many thanks in advance :slight_smile:.

Angel

As my experience with C#, it causes by your argument hoy.toString() maybe not correct date format (31/02/2020). Try again and make sure the input from the excel file hoy.toString() is correct

1 Like

@Angel_Llull

Check below post

Hope this helps you

Thanks

1 Like

Hello,

I have assigned as Date Time variable “VarDT = DateTime.Now” and in the activity to write in the excel file I have written = VarDT.ToString(“dd/MM/yyyy”).

By this way it has worked, even I am not sure if it is totally correct…

Many thanks!

Hello,

Many thanks for the link. I will check it! :slight_smile:

Angel

1 Like

Hi,

I suppose you need to set “M/d/yyyy” if your 9 or less day or month is expressed single digit.

As another solution, if you want just string exchanged day and month, the following expression will work.

 VarDT = System.Text.RegularExpressions.Regex.Replace(hoy.toString(),"(\d+)/(\d+)/(\d+)","$2/$1/$3")

Regards,