locked
String to DateTime Conversion RRS feed

  • Question

  • How to Convert a string to a DateTime object irrespective of regional setting of the system.

    I tried with the following code:

    DateTime dtTime = DateTime.ParseExact(strDateTime,"MM/dd/yyyy HH:m",null);

    I got the Exception: 

    System.FormatException: "String was not recognized as a valid date time";

    I have changed my regional setting to French,Italian and many others.

    So please suggest me a way by which the conversion will work independent of the CurrentCulture and CurrentUICulture.

    Tuesday, July 27, 2010 6:49 AM

Answers

  • Hi Gyana,

    You must use CultureInfo.InvariantCulture:

    using System;
    using System.Globalization;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          string strDateTime = "07/27/2010 10:41";
          DateTime dtTime = DateTime.ParseExact(strDateTime, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
          Console.Write(dtTime);
          Console.ReadKey(true);
        }
      }
    }
    
    

    For details about CultureInfo.InvariantCulture please refer to the following link.

    CultureInfo Class:
    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx

    Marcel

    • Proposed as answer by Nikunj Naik Tuesday, July 27, 2010 10:19 PM
    • Marked as answer by SamAgain Wednesday, August 4, 2010 6:12 AM
    Tuesday, July 27, 2010 8:44 AM
  • The Problem with TryParseExact is that if the regional setting is made to other then english it still as it has no idea of the regional setting.

    The way i found is use of DateTime Constructor.

    DateTime dt = new DateTime(int year,int month, int day, int hour, int min, int sec)..

    and it never gives error what ever the regional setting may be.

    So i got my problem solved with the use of it.

    • Marked as answer by Gyana Monday, August 9, 2010 11:11 AM
    Monday, August 9, 2010 11:11 AM

All replies

  • Hi,

    Please try this.

    DateTime dtTime = DateTime.ParseExact(strDateTime,"MM/dd/yyyy HH:m",System.Globalization.CultureInfo.CurrentCulture);


    Syed Mustehsan Ikram
    Tuesday, July 27, 2010 8:11 AM
  • Hi Gyana,

    You must use CultureInfo.InvariantCulture:

    using System;
    using System.Globalization;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          string strDateTime = "07/27/2010 10:41";
          DateTime dtTime = DateTime.ParseExact(strDateTime, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
          Console.Write(dtTime);
          Console.ReadKey(true);
        }
      }
    }
    
    

    For details about CultureInfo.InvariantCulture please refer to the following link.

    CultureInfo Class:
    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx

    Marcel

    • Proposed as answer by Nikunj Naik Tuesday, July 27, 2010 10:19 PM
    • Marked as answer by SamAgain Wednesday, August 4, 2010 6:12 AM
    Tuesday, July 27, 2010 8:44 AM
  • Hi,

    Just a few weeks ago there was also a question about converting a DateTime from a string. See this thread in the forums.

    The answer I proposed there was to use TryParseExact and use the overload that enables you to use multiple formats of the input string. Even though the end result is in the other case converted to a string by use of the ToString() method, the resulting DateTime is what you want.

    In your own example your passing null as the IFormatProvider. If you would want to use this code independent of CurrentCulture and CurrentUICulture, you should use CultureInfo.InvariantCulture.

    Hope this helps
    Meile Zetstra


    http://blogger.xs4all.nl/mzetstra

    Tuesday, July 27, 2010 8:48 AM
  • The Problem with TryParseExact is that if the regional setting is made to other then english it still as it has no idea of the regional setting.

    The way i found is use of DateTime Constructor.

    DateTime dt = new DateTime(int year,int month, int day, int hour, int min, int sec)..

    and it never gives error what ever the regional setting may be.

    So i got my problem solved with the use of it.

    • Marked as answer by Gyana Monday, August 9, 2010 11:11 AM
    Monday, August 9, 2010 11:11 AM