使用C#计算输入/输出数据的总时间

时间:2013-02-06 18:12:19

标签: c#

如何编写C#代码来计算Office中的总InTime,并在特定日期为特定人员提供多个输入/输出时间。我有一个用于选择人名的下拉列表和一个用于选择我需要的日期的日历获得员工的总InTime。

数据如下面的excel表xyz中所示:

 **Date          Event Description   Item Description**
12/21/2012 1:51 Exit granted    Ban Swapnil
12/21/2012 2:45 Entry granted   Ban Swapnil
12/21/2012 8:12 Exit granted    Ban Swapnil
12/25/2012 22:44    Entry granted   Ban Swapnil
12/26/2012 0:59 Exit granted    Ban Swapnil
12/26/2012 1:34 Entry granted   Ban Swapnil
12/26/2012 8:15 Exit granted    Ban Swapnil
12/26/2012 22:05    Entry granted   Ban Swapnil
12/26/2012 23:46    Exit granted    Ban Swapnil
12/21/2012 1:24 Exit granted    Bandel Ashish Chander
12/21/2012 15:23    Entry granted   Bandel Ashish Chander
12/21/2012 15:34    Exit granted    Bandel Ashish Chander
12/21/2012 15:51    Entry granted   Bandel Ashish Chander
12/21/2012 16:06    Exit granted    Bandel Ashish Chander
12/21/2012 16:29    Entry granted   Bandel Ashish Chander
12/21/2012 17:56    Exit granted    Bandel Ashish Chander
12/21/2012 18:32    Entry granted   Bandel Ashish Chander
12/21/2012 19:03    Exit granted    Bandel Ashish Chander
12/21/2012 19:37    Entry granted   Bandel Ashish Chander
12/21/2012 21:40    Exit granted    Bandel Ashish Chander
12/21/2012 21:50    Entry granted   Bandel Ashish Chander
12/21/2012 23:01    Exit granted    Bandel Ashish Chander
12/24/2012 13:39    Entry granted   Bandel Ashish Chander
12/24/2012 13:41    Exit granted    Bandel Ashish Chander
12/24/2012 14:52    Entry granted   Bandel Ashish Chander
12/24/2012 15:54    Exit granted    Bandel Ashish Chander
12/24/2012 16:41    Entry granted   Bandel Ashish Chander
12/24/2012 18:31    Exit granted    Bandel Ashish Chander
12/24/2012 22:07    Entry granted   Bandel Ashish Chander
12/24/2012 23:00    Exit granted    Bandel Ashish Chander
12/25/2012 14:00    Entry granted   Bandel Ashish Chander
12/25/2012 14:02    Exit granted    Bandel Ashish Chander
12/25/2012 14:48    Entry granted   Bandel Ashish Chander
12/25/2012 18:45    Exit granted    Bandel Ashish Chander
12/26/2012 13:14    Entry granted   Bandel Ashish Chander
12/26/2012 13:29    Exit granted    Bandel Ashish Chander
12/26/2012 14:33    Entry granted   Bandel Ashish Chander
12/26/2012 17:45    Exit granted    Bandel Ashish Chander

1 个答案:

答案 0 :(得分:2)

这很简单。你可能只是没想过。您可以将两个日期之间的差异视为TimeSpan。这意味着您可以执行date1 - date2并获得TimeSpan结构,该结构将为您提供小时/分钟/秒。

现在,假设您对给定用户有一堆 out 次,您需要一个对进出时间列表即可。 2-tuple非常适合这项工作,所以你会得到一个2元组的列表。

给定一个进/出时间对的地图,你可以总计TimeSpans。这是一个例子:

(注意您的数据对我没有意义,但如果您可以从数据中生成以下列表,那么您的问题陈述就会解决。)

List<Tuple<DateTime,DateTime>> lstForOnePerson = ..
int totalHours = 0;
foreach(var tup in lstForOnePerson)
{
 //this is assuming the first item in the tuple is IN time and the second is OUT
 TimeSpan tm = tup.Item2 - tup.Item1;
 totalHours += tm.TotalHours;
}