0

I am reading from excel file and save the content into database

one of the column contains length of video in this format

HH:mm:ss

I write this code so far

string time = oledbReader[6].ToString();
DateTime streamingTime = DateTime.ParseExact(time, 
                             "HH:mm:ss",
                             System.Globalization.CultureInfo.CurrentCulture);

I am getting error:

String was not recognized as a valid DateTime.

I tried debug mode and I see the value :"30/12/1899 00:09:21" in the Variable time when the value in the current execl column is:"00:09:21"

Where does the "30/12/1899" came from? Why is the string was not recognized as a valid DateTime?

Can I save only the format HH:mm:ss into sql server?

4

4 回答 4

2

试试这个,就像我上面的评论一样简单。

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-29T18:18:40.487 回答
2

由于您没有向我们提供有关您的任何信息CultureInfo,因此这里有InvariantCulture;

string time = "30/12/1899 00:09:21";
DateTime streamingTime = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(streamingTime);

输出将是;

12/30/1899 12:09:21 AM

这里一个DEMO.

欲了解更多信息,请查看Custom Date and Time Format Strings

于 2013-09-29T18:20:20.307 回答
1

使用TimeSpan结构来保存时间值。ADateTime包括日期和时间。

于 2013-09-29T18:43:51.243 回答
0

问题是excel没有时间字段并自动将时间转换为日期时间字段。如果课程日期为“空”,这意味着它是 excel 的最小日期 - 1899 年 12 月 30 日

此外,您不必调用 ToString 方法,因为对象已经是 DateTime

于 2013-09-29T18:28:39.640 回答