DateTime.ParseExact Doesn't Work

Hi,

Don’t know why my DateTime.ParseExact call doesn’t work, anyone please help. Thanks.

Activity 1: Assign
sTmp = “5/4/2020 10:54 PM”

Activity 2: Assign
oDateTime = DateTime.ParseExact( sTmp, “dd/MM/yyyy hh:mm tt”, System.Globalization.CultureInfo.InvariantCulture)

Where sTmp is String and oDateTime is DateTime

When I test it in Studio, i got the following error
Assign: String was not recognized as a valid DateTime.

Version:
Here is my version info:

Studio Pro 2020.10.2 - 10/23/2020
Community License
EXE Installer

Update Channel: Stable

Microsoft Windows 10 Enterprise 64-bit
.NET Framework Version 4.8 or later

I also uploaded the project for your easier reference.
TestConversion.zip (33.4 KB)

BR
Calvin

1 Like

@calvinsz
Welcome to the forum

have a check on following:
sTmp = “5/4/2020 10:54 PM”

DateTime.ParseExact( sTmp, “dd/MM/yyyy hh:mm tt”, System.Globalization.CultureInfo.InvariantCulture)

is defining a 2digit format and so not matching the one digit day, month 5/4

give a try on following:
DateTime.ParseExact( sTmp, “d/M/yyyy hh:mm tt”, System.Globalization.CultureInfo.InvariantCulture)

1 Like

It works! Thanks.

@calvinsz
Perfect just do some further testing and when it passed the tests just close the opic with marking the solving post as solution. Thanks

After further testing, I finally found out the root cause of my issue.

I got caught by asc 63. The string I passed to the Parse function contained the invisible character(which is asc 63).

After I loop the string and remove the chr, the code works well.

Thanks again for your help.

1 Like

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