locked
Datetime format Issue String was not recognized as a valid DateTime RRS feed

  • Question

  • am reading a date from an excel file, with this date format into an sql table

    6/04/2014 11:59:46PM

    this is a snippet of my code

    n.Date = DateTime.ParseExact(dRow[4].ToString(), "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

    the date column is on the 5th column on the excel sheet(dRow[4])

    Tuesday, May 27, 2014 12:36 PM

Answers

  • Try the following

    DateTime.ParseExact(dRow[4].ToString(), "dd\\/MM\\/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

    Also note that as you use "dd", day should contain 2 chars like '06'


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    • Marked as answer by ajibolao Monday, June 2, 2014 8:17 AM
    Wednesday, May 28, 2014 10:38 AM

All replies

  • Try this

              string dateStr = "6/04/2014 11:59:46PM";
                DateTime dt = DateTime.ParseExact(dateStr, "d/MM/yyyy HH:mm:sstt", CultureInfo.InvariantCulture);


    jdweng

    Tuesday, May 27, 2014 4:10 PM
  • The following code should work (you need to use lowercase hh to specify that the time uses a 12-hour clock and tt to parse the AM/PM part):

    n.Date = DateTime.ParseExact(dRow[4].ToString(), "d/MM/yyyy hh:mm:sstt", CultureInfo.InvariantCulture);
    

    • Proposed as answer by Ashish Pandey Wednesday, May 28, 2014 2:01 PM
    Tuesday, May 27, 2014 9:16 PM
  • Hi,

    if you want to convert the string to DateTime simply use it like this...

    var s = Convert.ToDateTime(dRow[4].ToString());
    Did you tried this?


    Thanks!
    Sundar
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Wednesday, May 28, 2014 6:54 AM
  • Try the following

    DateTime.ParseExact(dRow[4].ToString(), "dd\\/MM\\/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

    Also note that as you use "dd", day should contain 2 chars like '06'


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    • Marked as answer by ajibolao Monday, June 2, 2014 8:17 AM
    Wednesday, May 28, 2014 10:38 AM