简体   繁体   中英

String was not recognized as a valid DateTime ParseExact Error

I am trying to convert a datarow item to a datetime then i only want to show the Hours and Mins.

When i run this i get the following error "String was not recognized as a valid DateTime." Code

 DateTime PushinValue = DateTime.ParseExact(dataRow[0].ToString(), "HH:mm", null);

dataRow[0] is a DateTime

What is causing the Error does anyone know any ways to get around this error.

I have tried to do this with .Parse Ect.

Any help at all appricated.

尝试这个

String _mydatestring = DateTime.ParseExact(dataRow[0].ToString,"dd.MM.yyyy HH:mm:ss").ToString("HH:mm")

You are trying to parse "28.03.2013 13:11:28" to "hh:mm" :-) Why do you parse DateTime into DateTime then format it?

Why not simply this:

        DateTime dt = DateTime.Now;
        Debug.WriteLine(dt.ToString("hh:mm"));

If you're looking to just get the time element in the HH:mm format, then;

DateTime PushinValue = DateTime.Parse(dataRow[0].ToString());
    String myTime = PushinValue.ToString("HH:mm");

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