none
DateTime tryParse problem RRS feed

  • Question

  • Hi,

    I really don't know what is wrong. I have problem to parse string to datetime. I use this method :

    DateTime dt;
    string date = "14.5.2011";
    bool b = DateTime.TryParse(date, out dt);
    Console.WriteLine("bool = " + b);

    It show me always false. Do you know why? I have never had problem with this.

    Thank you for answer.

    Monday, September 25, 2017 7:23 AM

All replies

  • it shows tru when a string is in this format ...

    string date = "yyyy.M.dd";
    
    so
    
    string date = "2011.5.14";

    I still dont know how to do it ...

    maybe tryParse with many formats or ...

    Monday, September 25, 2017 7:39 AM
  • The behaviour depends on current language (culture) of Operating System and your program. You can change it programmatically using a line like this (before your first example):

       CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo( "ro" );

    Use the required culture name, which can be found in the documentation for GetCultureInfo.

    But see also TryParseExact, where you can specify the format.


    • Edited by Viorel_MVP Monday, September 25, 2017 7:53 AM
    Monday, September 25, 2017 7:49 AM
  • If someone can, please give me example that works ...

    I am aslo trying to use ParseExact ... but no change

    bool b = DateTime.TryParseExact(date, "dd.MM.yyyy",
             CultureInfo.InvariantCulture,
             DateTimeStyles.None,
             out dt);

    Monday, September 25, 2017 8:01 AM
  • I am also trying this :
    string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
                        "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
                        "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
                        "M/d/yyyy h:mm", "M/d/yyyy h:mm",
                        "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                        "MM/d/yyyy HH:mm:ss.ffffff","dd.MM.yyyy" };
    
    for(int i=0; i<formats.Length; i++)
    {
        bool b = DateTime.TryParseExact(date, formats[i],
                 CultureInfo.InvariantCulture,
                 DateTimeStyles.None,
                 out dt);  
        Console.WriteLine(b);
    }
    still b = false ... on all lines

    Monday, September 25, 2017 8:10 AM
  • Hi ! 

    By default the dateformat needs 2 digits for a month , if you want to allow one digit=> use another dateformat.

    You could also change it to 14.05.2011 instead of 14.5.2011

    code sample with dateformat dd.m.yyyy 


      class Program
        {
            
            static void Main(string[] args)
            {
                 
                DateTime date;
                string strdate = "14.5.2011";
                bool b = DateTime.TryParseExact(strdate, "dd.M.yyyy",
                               CultureInfo.InvariantCulture,
                               DateTimeStyles.None,
                               out date);
                Console.WriteLine("bool = " +b);
            }
        }

    kind regards


    Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.











    Monday, September 25, 2017 12:30 PM
  • Hello TakeshiKitano,

    Is there any updates?If you have any issues with the replies , please feel free to contact me . And if the issue has been solved ,you could mark the useful reply as answer,This can be beneficial to other community members reading this thread.

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 6, 2017 6:46 AM
    Moderator