简体   繁体   中英

DateTime TryParse Exact returning 0001:01:01

I'm trying to parse DateTime string '28/3/2014 with method:

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd'/'MM'/'yyyy",
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None,
                       out d);

And its always returning me 0001:01:01 .

Can you tell how to set conversion parameters? I tried setting

System.Globalization.CultureInfo.CurrentCulture
System.Globalization.DateTimeStyles.AssumeLocal

With no effects

28/3/2014 is a example. Date could be also like 28/12/2014

Your pattern must be

"dd'/'M'/'yyyy"

Refer to the article Custom Date and Time Format Strings for more information.

Your input string month has no leading 0, so you can try this:

DateTime d;
DateTime.TryParseExact("28/03/2014", "dd/MM/yyyy", 
                        System.Globalization.CultureInfo.InvariantCulture,
                        System.Globalization.DateTimeStyles.None, out d);

or

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd/M/yyyy", 
                        System.Globalization.CultureInfo.InvariantCulture,
                        System.Globalization.DateTimeStyles.None, out d);

Hamlet Hakobyans' answer is right. But I want to add some explanation at least.

Let's analyze your question step by step:

You said your string is '28/3/2014 but you used 28/3/2014 in your example. I assume, 28/3/2014 is the right string for your case.

So, your real code is;

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd'/'MM'/'yyyy", 
                        CultureInfo.InvariantCulture, 
                        System.Globalization.DateTimeStyles.None,
                        out d);

In such a case, you don't need to use ' for your / delimiters but this doesn't break your code. Because ' is a literal string delimiter and it's still valid for such a case.

But MM specifier is for 01 to 12 , that doesn't fit for month value which is 3 . You should use M specifier which is for 1 to 12 .

And its always returning me 0001:01:01

Because your TryParseExact returns false (because your format doesn't fit with your string) and from it's documentation ;

result

Type: System.DateTime

When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed .

0001:01:01 is equal to DateTime.MinValue field (and other parts also..). That's why you get this value.

Finally, your code should be;

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd/M/yyyy",
                        CultureInfo.InvariantCulture,
                        DateTimeStyles.None,
                        out d);

And if you want 28/3/2014 as string ( DateTime does not have a format) a result, you have a few options;

If your current thread culture's DateSeperator is / and ShortDatePattern is dd/m/yyyy , you don't need to use anything. Just;

Console.WriteLine(d); //This will be enough.

If it is not, you can use DateTime.ToString(String, IFormatProvider) overload with InvariantCulture like;

Console.WriteLine(d.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)); 

Or you can escape your / no matter which culture you use like;

Console.WriteLine(d.ToString("dd'/'M'/'yyyy"));

or

Console.WriteLine(d.ToString(@"dd\/M\/yyyy"));

Try this:

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d);

The problem is the format of the input number for months. You have set the format to "MM" but you pass as parameter only 3 . Try 28/03/2014 ("03" instead of simple "3"). By the way, the format string can be simply "dd/MM/yyyy" instead of "dd'/'MM'/'yyyy". Check it on http://dotnetfiddle.net/I3Dyod

I accidentally left off the seconds part and was trying to translate this: DateTime.TryParse(@"1/1/2000 00:00:000", out DateTime dtLastRun);

It should have been: DateTime.TryParse(@"1/1/2000 00:00:00:000", out DateTime dtLastRun);

What I can't figure out why is DateTime was returning a DateTime2 response. But regardless, if you have an invalid string you will get the default time returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM