Date conversion - works only with some dates!

Hi all!

I have an interesting issue with date time assigning. I use this sentence in the assign activity, but it only works for some dates… The assign action is:

Convert.ToDateTime(Datetime.ParseExact(MYSTRING,“yyyy-MM-dd hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd-MM-yyyy”))

When the MYSTRING is: 2021-08-28 00:00:00 → this doesn’t work and gives error: string was not recognized as valid DateTime. BUT when the string is: 2021-08-01 00:00:00 → everything goes perfectly.

Cheers if you can help me!

Hi @Anonymous_Hippo,
try with Convert.ToDateTime(Datetime.ParseExact(mystring,“yyyy-MM-dd hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture))
It worked for me:

1 Like

Your code to convert the string to datetime looks fine! Sometimes there can be issues with confusing months and days (robot expects a month value but gets 28 - not valid; robot expects a month value and gets 01 - valid) but this doesn’t even look like the case here.

One possibility is an unexpected whitespace character somewhere in the string. Make sure to trim your string, before you try to convert it.

1 Like