从表中提取时间数据并将其放入TimeSpan

时间:2012-03-27 01:22:56

标签: c# .net mysql

在表中,any1知道如何选择指定的行并在指定的列中获取Time数据,然后将数据放入TimeSpan吗?

DataRow[] selectIDRow = RetailCamDataSet1.Tables["smBranchWorkingDayInfo"].Select("ID=" + ID);
foreach (DataRow row in getTimeDifference)
{
    TimeSpan startTime = new TimeSpan(); //Need to put the data into the bracket instead of using hard code

     TimeSpan endTime = new TimeSpan(20, 00, 00); //Hard coded
     TimeSpan timeDifference = new TimeSpan();

     timeDifference = endTime.Subtract(startTime);

     double minutes = timeDifference.TotalMinutes;

     normalCount = minutes / 15;

3 个答案:

答案 0 :(得分:1)

你试过TimeSpan.Parse()吗?

DataRow[] selectIDRow = RetailCamDataSet1.Tables["smBranchWorkingDayInfo"].Select("ID=" + ID);
    foreach (DataRow row in getTimeDifference)
    {
         DateTime dateTime = DateTime.Parse(row["DateTimeColumn"].ToString());
         TimeSpan timeSpan = TimeSpan.Parse(dateTime.ToString("hh:mm:ss"));
         ... //do whatever you want to do with timeSpan
    }

答案 1 :(得分:1)

如果您确定时间列的文本格式正确,则可以使用TimeSpan.Parse()

TimeSpan startTime = TimeSpan.Parse(row["time"].ToString());

答案 2 :(得分:0)

是的,我使用的是TimeSpan.Parse()。这是我的代码,它的工作原理! :)

foreach (DataRow row in RetailCamDataSet1.Tables["smBranchWorkingDayInfo"].Select("ID=" + convertedBranchID))
                {
                    var sTime = DateTime.Parse(row["SunFromTime"].ToString());
                    int sHour = sTime.Hour,
                        sMinute = sTime.Minute,
                        sSecond = sTime.Second;

                    var eTime = DateTime.Parse(row["SunToTime"].ToString());
                    int eHour = eTime.Hour,
                        eMinute = eTime.Minute,
                        eSecond = eTime.Second;

                    TimeSpan startTime = new TimeSpan(sHour, sMinute, sSecond);
                    TimeSpan endTime = new TimeSpan(eHour, eMinute, eSecond);
                    TimeSpan timeDifference = new TimeSpan();

                    timeDifference = endTime.Subtract(startTime);

                    double minutes = timeDifference.TotalMinutes;

                    normalCount = minutes / 15;