[英]Cannot Convert System.Datetime to string error
I have this code, but I get an error:我有此代码,但出现错误:
Cannot convert'System.DateTime' to 'string'.
无法将“System.DateTime”转换为“string”。
I'm trying to fetch only time part from a date as HH:MM
format.我正在尝试仅从日期中获取时间部分作为
HH:MM
格式。
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
.我认为您误解了
ParseExact
的使用。 The format parameter tells .NET the format of the input, not the format of the output. format 参数告诉 .NET 输入的格式,而不是输出的格式。 The format of the output is always the same-- a binary DateTime object that contains both date and time.
输出的格式始终相同——一个包含日期和时间的二进制 DateTime 对象。
To convert it back to a string you can use ToString()
, passing a format specifier to emit only the hours and minutes:要将其转换回字符串,您可以使用
ToString()
,传递格式说明符以仅发出小时和分钟:
q.CIP_START_TIME = DateTime.Parse(q.CIP_START_TIME).ToString("HH:mm");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.