"Assign: String was not recognized as a valid DateTime."

Hi everyone,

I’ve been trying to figure this one out for a while now but don’t get it to work. I hope somebody here can help me solve the problem.

I’m trying to convert a string to a DateTime type.

My process starts with the Get Text Activity which saves the text (e.g. “January 2021”) from a website in the variable jDate.

This works out just fine and I can write out a line in the console which prints out “January 2021”

Next, I’d like to convert this string to the DateTime format. I used the assign activity and created a variable called dateInput of the type DateTime for that:

dateInput = DateTime.ParseExact(jDate, “MMMM yyyy”, System.Globalization.CultureInfo.InvariantCulture)

Here I get the error: “Assign: String was not recognized as a valid DateTime.”

I tried the same process without Get text activity and I assigned the variable jDate the value January 2021 and it worked fine. Is it possible that the GetText activity doesn’t save it as a string but a generic value and has therefore to be coverted? If yes, please let me know how to do that.

Does anyone know how to solve this issue?

Cheers Tim

Can you try to trim jDate first? Maybe there are some extra spaces in the beginning or in the end

1 Like

Also instead of using InvariantCulture you could use a specific culture. e.g.
DateTime.ParseExact(“January 2021”, “MMMM yyyy”,new System.Globalization.CultureInfo(“us-US”))

DateTime requires day, month and year. Where is your day?

Force some day to your string:

DateTime.ParseExact(jDate + " 01", "MMMM yyyy dd", CultureInfo.InvariantCulture)

Day is not required for ParseExact to work. The OP’s code is correct. It seems like there is some extra string in the value he gets from Get Text activity.

@KannanSuresh you are right!

It automatically assumes the day 01.

Trimming worked perfectly. Thanks for the help!

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