How to convert String variable to DateTime variable - "String is not recognized as a valid datetime"

Hello, everyone,

I want to convert the following string to a DateTime.

strVar = “Wed Jan 01 2020 01:00:00 GMT+0100 (W. Europe Standard Time)”

I have tried the following functions but I get an error every time.

Error Message: “String is not recognized as a valid datetime”

DateTime_PeriodBegin = Convert.ToDateTime(“Wed Jan 01 2020 01:00:00 GMT+0100 (W. Europe Standard Time)”)

Does the community know another way I could try?

@IvanKurtovic

Check below screenshot

Mark as solution if this helps

Thanks

3 Likes

@IvanKurtovic - Please check the below link for date time with timezone conversion along with sample -

Im getting this error message when i try to run your code

@IvanKurtovic

Check below

DateTime.ParseExact(
sample,
“ddd MMM dd yyyy HH:mm:ss ‘GMT+0100 (W. Europe Standard Time)’”,
CultureInfo.InvariantCulture).ToString

Copy exact, as you are missing quotes

Thank you

I tried it exactly with your string. But i get the same Error.

Here is my Workflow

ParseDates.xaml (6.5 KB)

This is the Error message i get:

18.4.1+Branch.master.Sha.bc66386dd8e2160b6d35f31d5ce934a5aa4cf4bd

Source: Assign

Message: String was not recognized as a valid DateTime.

Exception Type: System.FormatException

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at lambda_method(Closure , ActivityContext )
at Microsoft.VisualBasic.Activities.VisualBasicValue1.Execute(CodeActivityContext context) at System.Activities.CodeActivity1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity1 expressionActivity) at System.Activities.InArgument1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate) at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

@IvanKurtovic

This is because you are not passing the sample date, you have to pass the sample Date as

Wed Jan 01 2020 01:00:00 GMT+0100 (W. Europe Standard Time)

Then you are fine to go

Check attached
ParseDates.xaml (5.7 KB)
project.json (1003 Bytes)

Mark as solution if this helps

Thanks

3 Likes

Thank you very much.
Your Solution is working :slight_smile:

1 Like

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