locked
String was not recognized as a valid DateTime. RRS feed

  • Question

  • My application reading the data from .CSV file and updating that into DB. From input file one column will be converted in to data time, if i get the 8 digits value that is converting to datatime format as expectedly. But if i get seven digits i am getting "System.FormatException: String was not recognized as a valid DateTime" ERROR message. but my requirement is to convert 3132009  as MMddyy format.
    Thursday, July 14, 2011 8:00 AM

Answers

  • DateTime has an overload of the ParseExact method which takes a format string as a parameter, so you should be able to do something like:

    var dateString = "3132009";
    var date = DateTime.ParseExact("Mddyyyy", CultureInfo.InvariantCulture);
    

    The M should handle months like 03 and 3.

    Evan

     

    • Proposed as answer by Kris444 Thursday, July 14, 2011 10:54 AM
    • Marked as answer by Min Zhu Tuesday, July 19, 2011 3:51 AM
    Thursday, July 14, 2011 8:06 AM
  • you have just problem with this dates which i list below, and there is no way to fix your dates cuz you cant say if its 11 january or 1 november 1.11.2009 or 11.1.2009

    so the dates which is below you should find an other way to correct your date.

    at the other dates,Evan's suggestion could work, but you could write your own datetime conversion easyly too.

    if the first number is bigger then 3 it is in dMMyyyy format,

    else if if the second number is bigger then 2 then its in ddmyyyy format

    else if 3. number is bigger then 2 then its in ddmyyyy format,

     

    in other cases which is listed below you have to correct your data.

     

    1112009
    2112009
    3112009
    1122009
    2122009


    I'm so sure you will not press to "Mark As Answer". and "Mark as Helpful".
    • Proposed as answer by Esref Durna Thursday, July 14, 2011 9:11 AM
    • Marked as answer by Min Zhu Tuesday, July 19, 2011 3:51 AM
    Thursday, July 14, 2011 9:07 AM

All replies

  • DateTime has an overload of the ParseExact method which takes a format string as a parameter, so you should be able to do something like:

    var dateString = "3132009";
    var date = DateTime.ParseExact("Mddyyyy", CultureInfo.InvariantCulture);
    

    The M should handle months like 03 and 3.

    Evan

     

    • Proposed as answer by Kris444 Thursday, July 14, 2011 10:54 AM
    • Marked as answer by Min Zhu Tuesday, July 19, 2011 3:51 AM
    Thursday, July 14, 2011 8:06 AM
  • you have just problem with this dates which i list below, and there is no way to fix your dates cuz you cant say if its 11 january or 1 november 1.11.2009 or 11.1.2009

    so the dates which is below you should find an other way to correct your date.

    at the other dates,Evan's suggestion could work, but you could write your own datetime conversion easyly too.

    if the first number is bigger then 3 it is in dMMyyyy format,

    else if if the second number is bigger then 2 then its in ddmyyyy format

    else if 3. number is bigger then 2 then its in ddmyyyy format,

     

    in other cases which is listed below you have to correct your data.

     

    1112009
    2112009
    3112009
    1122009
    2122009


    I'm so sure you will not press to "Mark As Answer". and "Mark as Helpful".
    • Proposed as answer by Esref Durna Thursday, July 14, 2011 9:11 AM
    • Marked as answer by Min Zhu Tuesday, July 19, 2011 3:51 AM
    Thursday, July 14, 2011 9:07 AM