[ACCEPTED]-DateTime.TryParse converts decimal to datetime-tryparse

Accepted answer
Score: 10

The following line of code return true (which 36 it should not)....and convert 1.0228 into 35 datetime...

DateTime.TryParse(1.0228,out temporaryDateTimeValue)

This will not compile.

However, if 34 you wrap it in quotes (and clean it up a 33 little bit),

bool success = DateTime.TryParse("1.0228", out temporaryDateTimeValue);

then, yes, you will get true back. You 32 need to read the documentation to understand 31 why, but basically, there are many different 30 ways to format dates and you stumbled on 29 one (maybe M.yyyy?).

If you don't want it to parse, may 28 I suggest

bool success = DateTime.TryParseExact(
                   "1.0228",
                   "yyyyMMdd", 
                   CultureInfo.InvariantCulture,
                   DateTimeStyles.None,
                   out temporaryDateTimeValue
               );

Then success is false.

I note from the remarks 27 in the documentation:

The string s is parsed using formatting 26 information in the current DateTimeFormatInfo object, which 25 is supplied implicitly by the current thread 24 culture.

This method tries to ignore unrecognized 23 data, if possible, and fills in missing 22 month, day, and year information with the 21 current date. If s contains only a date 20 and no time, this method assumes the time 19 is 12:00 midnight. Any leading, inner, or 18 trailing white space character in s is ignored. The 17 date and time can be bracketed with a pair 16 of leading and trailing NUMBER SIGN characters 15 ('#', U+0023), and can be trailed with one 14 or more NULL characters (U+0000).

Because 13 the DateTime.TryParse(String, DateTime) method tries to parse the string representation 12 of a date and time using the formatting 11 rules of the current culture, trying to 10 parse a particular string across different cultures 9 can either fail or return different results. If 8 a specific date and time format will be 7 parsed across different locales, use the 6 DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) method or one of the overloads of the TryParseExact method 5 and provide a format specifier.

Basically, TryParse "tries" very 4 hard to parse the string you give it (although 3 the "Try" really refers to the fact 2 that the method returns a bool for success/failure 1 indication).

Score: 4

No, that code doesn't return true - it doesn't 12 even compile:

using System;

class Program
{

    static void Main(string[] args)
    {
        DateTime dt;
        Console.WriteLine(DateTime.TryParse(1.0228, out dt));
    }
}

Error:

Test.cs(9,27): error CS1502: The best overloaded method match for
        'System.DateTime.TryParse(string, out System.DateTime)' has some invalid
        arguments
Test.cs(9,45): error CS1503: Argument 1: cannot convert from 'double' to
        'string'

If you change it to "1.0228" it 11 does return true, yes. It looks like it's using 10 a format of "M.yyyy", which is 9 no doubt valid for some cultures... and 8 highlights why it's a bad idea to use DateTime.TryParse in 7 my view. If you've got a specific format 6 (or set of formats) in mind, you should 5 use DateTime.TryParseExact instead so you can specify the format.

I 4 usually find it's a good idea to specify 3 the exact format, and I usually also specify CultureInfo.InvariantCulture unless 2 the date is coming directly from the user 1 (which is rare, in my experience).

More Related questions