简体   繁体   中英

Cannot Convert System.Datetime to string error

I have this code, but I get an error:

Cannot convert'System.DateTime' to 'string'.

I'm trying to fetch only time part from a date as HH:MM format.

IFormatProvider provider = CultureInfo.InvariantCulture;

q.CIP_START_TIME = DateTime.ParseExact(q.CIP_START_TIME, "hhmm", provider);
q.CIP_END_TIME = DateTime.ParseExact(q.CIP_END_TIME, "hhmm", provider);
q.CIP_VRB_TIME = DateTime.ParseExact(q.CIP_VRB_TIME, "hhmm", provider);

I think you're misunderstanding the use of ParseExact . The format parameter tells .NET the format of the input, not the format of the output. The format of the output is always the same-- a binary DateTime object that contains both date and time.

To convert it back to a string you can use ToString() , passing a format specifier to emit only the hours and minutes:

q.CIP_START_TIME = DateTime.Parse(q.CIP_START_TIME).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