我正在从excel文件中读取内容并将其保存到数据库中。
其中一列包含此格式的视频长度。
HH:mm:ss
到目前为止,我编写了这段代码
string time = oledbReader[6].ToString();
DateTime streamingTime = DateTime.ParseExact(time,
"HH:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);我正在犯错误:
字符串不能被识别为有效的DateTime。
我尝试了调试模式,当当前execl列中的值为“00:09:21”时,我在变量time中看到值:"30/12/1899 00:09:21“
“1899年12月30日”是从哪里来的?为什么字符串不能被识别为有效的DateTime?
我能否只将HH:mm:ss格式保存到sql服务器中?
发布于 2013-09-29 18:18:40
试试这个,就像我上面的评论一样简单。
string time = oledbReader[6].ToString().Split(" ".ToCharArray())[1];
DateTime streamingTime = DateTime.ParseExact(time, "HH:mm:ss",System.Globalization.CultureInfo.CurrentCulture);或者你可以按原样解析..。
DateTime streamingTime = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);发布于 2013-09-29 18:43:51
使用TimeSpan结构保存时间值。DateTime包括日期和时间。
发布于 2013-09-29 18:28:39
问题是excel没有时间域,并自动将时间转换为日期时间字段。如果课程日期为“空”,这意味着它是excel - 30/12/1899的最小日期。
此外,您不必调用ToString方法,因为对象已经是一个DateTime
https://stackoverflow.com/questions/19081524
复制相似问题