Convert String taken from DataTable to DateTime Variable

Hi everyone!
First time posting on the forum for me.

I’m working on an automation that requires me to convert a date as a String variable taken from a DataTable into a DateTime Variable, to later calculate and see if 14 days have passed since that date.
The problem I’m encountering is that I can’t in any way make the DateTime.ParseExact function work.
The start date gets pulled from the datatable in the format “MM/dd/yyyy HH:mm:ss” and gets sanitized via RegEx to only output the “MM/dd/yyyy” part.

strAssignmentDate = System.Text.RegularExpressions.Regex.Match(CurrentRow.Item(“DATAINC”).ToString, “(0[1-9]|1[012])- /.- /.\d\d”).ToString

Then I made an Assign activity which contains:

dtimeAssignmentDate = DateTime.ParseExact(“strAssignmentDate”, “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture)

But it always gives me the error (yes, even if I leave strAssignmentDate as “MM/dd/yyyy HH:mm:ss”)

Assignemt Date: 01/08/2024
Multiple Assign: Can not assign ‘DateTime.ParseExact(“strAssignmentDate”, “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture)’ to ‘dtimeAssignmentDate’.
A fatal error has occurred. Data couldn’t be processed.
Workflow | Main - DataProcess | ended with 1 errors.


Everything is in a For Each DT Row

Hi @tommaso.perina

Could you share the Excel with sample data.
I think you are getting the date into an variable said strAssignmentDate so it should not passed within double quotes.

DateTime.ParseExact(strAssignmentDate.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)’ to ‘dtimeAssignmentDate

Regards

1 Like

Hi @tommaso.perina

Just a quick checking, try taking out the double quota.
image

image

That was the problem. I am so sorry.
I tried for hours, almost lost my mind and assumed I did something wrong in the code but that was the issue.
Thank you so much for pointing it out!

That was indeed the issue, thank you all.

Hi @tommaso.perina

Try the below syntaxes. It should work.

strAssignmentDate = System.Text.RegularExpressions.Regex.Match(CurrentRow.Item("DATAINC").ToString, "\d*\/\d*\/\d*").Value
dtimeAssignmentDate = DateTime.ParseExact(strAssignmentDate.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

Regards

1 Like

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