Error in converting String to Date time...

csharp , windows forms , date conversion India
  • 11 years ago

    I'm trying to convert a string value(19/3/2009) by using Convert.Datetime("19/3/2009",CultureInfo("en-US")); The above given code works good in my System.When i tried to deploy the application.the same code give the below given error

    System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.String.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ToDateTime(Object value, IFormatProvider provider)

    Can anyone suggest me the reason for this

  • 11 years ago

    Hi,

    most likely the culture of the system you are installing it to doesn't match yours. it might be set to en-GB or have a different time/date seperator.

    Si

  • 11 years ago

    it still gives the error if i convert this without culture Convert.Datetime("19/3/2009") i need to convert 19/03/2009 to a valid Date time. Can anyone help on this ?

  • 11 years ago

    hi,

    you could try date.parseexact, but again, i have had mixed results with this, i ended up writing this routine to convert my dates, it has only failed me once so far.

    Public Shared Function MangleDate(ByVal TheDate As String) As Date        
            Dim strSystemFormat As String = System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern
            Dim strDay As String = TheDate.Substring(0, 2)
            Dim strMonth As String = TheDate.Substring(3, 2)
            Dim strYear As String = TheDate.Substring(6, 4)
            strSystemFormat = strSystemFormat.Replace("dd", strDay)
            strSystemFormat = strSystemFormat.Replace("MM", strMonth)
            strSystemFormat = strSystemFormat.Replace("yyyy", strYear)
            Return Date.Parse(strSystemFormat, Nothing, DateTimeStyles.AssumeLocal)
        End Function
    

    Try it and see how it goes for you

    Si

  • 11 years ago

    Perfect it worked good

  • 11 years ago

    Hi all ,

    This Code might solve your issue .

    CultureInfo provider = CultureInfo.InvariantCulture ;

    string dateTime = "20090303115521";

    string format = "yyyyMMddhhmmss";

    DateTime dtdateTime;

    dtdateTime = DateTime.ParseExact( dateTime, format, provider );

  • 11 years ago

    Hi all.. thanks for taking time to reply my post.. i found out the solution by setting thread culture. which resulted.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”