String was not recognized as a valid DateTime Conversion problem

Hi,
I am trying to read an excel range to a datatable and convert one of the Column value from String to DateTime type. My objective is to get the column format to match by SQL Server Table column datatype.
I tried-

  1. Convert.ToDateTime(DateTime.ParseExact(row.Item(3).ToString,“dd-MM-yyyy hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture).ToString(“YYYY-MM-DD hh:mm:ss.nnn”)
  2. DateTime.ParseExact(row.Item(3).ToString,“dd-MM-yyyy hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)
  3. Convert.ToDateTime(row.item(3).ToString)

My Excel Cell value is in “dd-MM-yyyy hh:mm AM/PM” format and i want it to convert to “YYYY-MM-DD hh:mm:ss.nnn”

Hi,

If your data is stored as String in worksheet, can you try the following?

DateTime.ParseExact(row.Item(3).ToString,"dd-MM-yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd HH:mm:ss.fff")

or if your data is stored as datetime in worksheet, the following might work.

DateTime.Parse(row.Item(3).ToString).ToString("yyyy-MM-dd HH:mm:ss.fff")

Regards,

1 Like

Thankyou so much @Yoichi. I am able to convert the string to datetime format now. it was problem in format.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.