Avatar of sepknow
sepknow asked on

DateTime.TryParse method always return false

Need advise why the DateTime.TryParse method always return false?
string[] mFormat = 
    { 
        "M/dd/yyyy hh:mm:ss tt", 
        "MM/dd/yyyy hh:mm:ss tt" 
    };
 
if (DateTime.TryParseExact("5/25/2008 3:43:04 PM", mFormat, null, System.Globalization.DateTimeStyles.None, out myDateTime))
{
}

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
Joruus10

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
josgood

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Joruus10

Hi!

This code works fine:

string[] mFormat =
    {
        "M/dd/yyyy hh:mm:ss",
        "MM/dd/yyyy hh:mm:ss"
    };
 
if (DateTime.TryParseExact("5/25/2008 03:43:04", mFormat, null, System.Globalization.DateTimeStyles.None, out myDateTime))
{
}

Without the PM/AM and with 2 digits in the hour component.
Probably tt (PM/AM) does not work with other culture info than en-US. Try changing it.

Good Luck

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck