Convert String to DateTime throws Error

Hi

I am trying to convert a String into DateTime Variable, but the following Error occoured: “The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar”

Here is what i did: (I replaced my Stringvalue with DateTime.now.toString, because it throws the same Error)

Log Message
DateTime.now.toString

Assingn
Test (DateTime) = DateTime.ParseExact(DateTime.now.ToString,“dd/MM/yyyy
HH:mm:ss”,system.globalization.CultureInfo.InvariantCulture)

Output:
Test started
28/07/2022 15:30:21
Assign: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar

Anyone knows a Fix for this?
Thanks in advance!

Why are you converting a datetime (DateTime.Now) to string and then trying to convert it back? This makes no sense. DateTime.Now gives you a datetime value.

1 Like

Hey!

You have to import the system.globalization.CultureInfo namespaces from imports panel.

And try this

Strinput = Now.ToString("dd/MM/yyyy hh:mm:ss tt")

The above expression will give you the system date and time in the mentioned format…

If you wants to convert this time to GregorianCalendar

Try this:

str_datetime = Datetime.ParseExact(Strinput.ToString,"dd/MM/yyyy hh:mm:ss tt",System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy hh:mm:ss", New CultureInfo("da-DK"))

Try this and let me know

Regards,
NaNi

i think you not need to change because datetime.now give you datetime only if you are trying to convert another format pin that please

Converting from datetime to string back to datetime and back to string accomplishes nothing.

Let us summarize what we did understand so far

you tried to parse a string and got the exception from above
for a cross check you tried instead of the variable now.ToString but got also the same exception from above

Kindly note:
grafik

when trying to parse it with:

DateTime.ParseExact(strYourVar,"dd/MM/yyyy HH:mm:ss"system.globalization.CultureInfo.InvariantCulture)

then please note the swap from day and month info
grafik

so check the format of your string and adapt it accordingly in your statement. We would recommend to do quick prototyping during debugging within the immediate panel

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

Hello @Skylon A small correction to your expression. Try this

dateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyyHH:mm:ss"),"dd/MM/yyyyHH:mm:ss",system.globalization.CultureInfo.InvariantCulture)

Thank u everyone!

I learnd a lot from all your Tipps!

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