Community discussion forum

Error in converting String to Date time...

  • 6 months 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

    Post was edited on 20/05/2009 11:31:35 Report abuse
  • 6 months 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

  • 6 months 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 ?

  • 6 months 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

  • 6 months ago

    Perfect it worked good

  • 5 months 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 );

  • 5 months 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).

Want to stay in touch with what's going on? Follow us on twitter!