vaibhav shah
vaibhav shah

Reputation: 5069

String not getting converted to datetime

I have I have Excel file from where I am getting date in string format as "30-12-1899 07:50:00:AM"

When I am trying to convert it to DATETIME then it is giving error as

String was not recognized as a valid DateTime

I am trying to convert it like this

Convert.ToDateTime(homeToSchool[7],new DateTimeFormatInfo { ShortDatePattern = "dd-MM-yyyy", DateSeparator = "-" })

Upvotes: 0

Views: 426

Answers (2)

Rohit
Rohit

Reputation: 10236

Since you are reading this from excel I hope this would help

 DateTime.FromOADate(homeToSchool[7].ToString("dd-MMM-yyyy");

Upvotes: 0

John Woo
John Woo

Reputation: 263703

Use DateTime.ParseExact

string myDate = "30-12-1899 07:50:00:AM";
DateTime dt1 = DateTime.ParseExact(myDate, "dd-MM-yyyy hh:mm:ss:tt", 
                                           CultureInfo.InvariantCulture)

For more information about Date and Time Format Strings,

Upvotes: 3

Related Questions