2012-03-27 56 views
0

在表格中,any1是否知道如何选择指定的行并在指定列中取时间数据,然后将数据放入TimeSpan?从表格中提取时间数据并将其放入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; 
+0

什么样的格式是指定列中的时间数据? – 2012-03-27 01:27:50

+0

小时:分钟:秒 hh:mm:ss – hakunabean 2012-03-27 01:30:57

回答

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 
    } 
+0

是的,我使用TimeSpan.Parse()。这是我的代码,它的工作原理! :) – hakunabean 2012-03-27 03:09:34

+0

Thx对于我们的信息,它也可以作为未来的参考。 – hakunabean 2012-03-27 09:47:43

1

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

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

确定thx为你的信息。 – hakunabean 2012-03-27 02:41:23

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; 
相关问题