简体   繁体   中英

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 = "-" })

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,

由于您正在阅读excel的内容,希望对您有所帮助

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM