如何获得AM和PM的值班时间与值班时间的差异?

时间:2016-01-25 15:45:26

标签: c# datatable

updated attendance data

我们坚持这个功能如何获得总时间列,我们想要的是,关税减去早上时间的值班时间,以及下午时间以便我们可以总和时间列的小时数。 我们希望将总数显示到最后一行时间列。

sample attendance data scrshot

//display attendance data from database
    private void btnShow_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ralph\Documents\User.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); ;

        // SqlDataAdapter adapSel;

        conn.Open();
        //SqlDataAdapter sda = new SqlDataAdapter("SELECT * from AMPS  where DateandTime >= '" + dtpFrom.Value.ToString("dd/MMM/yyyy") + "' AND DateandTime <= '" + dtpTo.Value.ToString("dd/MMM/yyyy") + "'", conn);
        SqlDataAdapter sda = new SqlDataAdapter("SELECT WorkersInfo.WorkersID, WorkersInfo.LName, WorkersInfo.FName, WorkersInfo.MName, WorkersInfo.Position, WorkersInfo.Salary, AMPS.InandOut, cast(DateandTime as date) [Date], RIGHT(CONVERT(VARCHAR, DateandTime, 100),7) as Time FROM AMPS INNER JOIN WorkersInfo ON AMPS.EnNo = WorkersInfo.WorkersID where DateandTime between '" + dtpFrom.Value.ToString("dd/MMM/yyyy") + "' and '" + dtpTo.Value.ToString("dd/MMM/yyyy") + "'", conn);
        DataGridViewColumn InandOut = grdList.Columns[6];
        InandOut.Width = 100;
        DataGridViewColumn DateandTime = grdList.Columns[7];//column DateandTime
        DateandTime.Width = 145;
        DataGridViewColumn MName = grdList.Columns[3];//column MiddleName
        MName.Width = 50;//changing the column width

        //da.Fill(ds);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        grdList.DataSource = dt;
        conn.Close();
    }

    private void DisplayData()
    {
        CON.Open();
        DataTable dt = new DataTable();
        adapt = new SqlDataAdapter("SELECT WorkersInfo.WorkersID, WorkersInfo.LName, WorkersInfo.FName, WorkersInfo.MName, WorkersInfo.Position, WorkersInfo.Salary, AMPS.InandOut, AMPS.DateandTime FROM AMPS INNER JOIN WorkersInfo ON AMPS.EnNo = WorkersInfo.WorkersID", CON);
        adapt.Fill(dt);
        grdList.DataSource = dt;
        CON.Close();
    }

    private void grdList_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        txtWorkersID.Text = grdList.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtLast_Name.Text = grdList.Rows[e.RowIndex].Cells[1].Value.ToString() + ", " + grdList.Rows[e.RowIndex].Cells[2].Value.ToString() + ", " + grdList.Rows[e.RowIndex].Cells[3].Value.ToString();
        txtPosition.Text = grdList.Rows[e.RowIndex].Cells[4].Value.ToString();
        txtDRate.Text = grdList.Rows[e.RowIndex].Cells[5].Value.ToString();
        //cboGender.Text = grdWorkers.Rows[e.RowIndex].Cells[5].Value.ToString();
    }

但我们仍然坚持如何计算从值班到值班的小时数或时差

1 个答案:

答案 0 :(得分:0)

您可以使用DateTime和TimeSpan来完成。您的SQL查询实际上将DateandTime对象拆分为单独的日期和时间列。您可以在C#中重建DateTime,或者只从查询中提取附加列。

using (GraphicsPath graphicsPath = new GraphicsPath())
{
    graphicsPath.AddString(
        "sample text",
        new FontFamily("Times New Roman"),
        (int)FontStyle.Bold,
        graphics.DpiY * 12 / 72,
        new PointF(0,0),
        StringFormat.GenericDefault
    );

    Matrix matrix = new Matrix();
    matrix.Shear(10, 0);
    graphics.MultiplyTransform(matrix);

    graphics.FillPath(new SolidBrush(Color.Red), graphicsPath);
}

然后您可以访问:

DateTime timeOut = x;//use your dateandtime column value here for time out
DateTime timeIn = y;//same here for time in

TimeSpan timeSpan = timeOut.Subtract(timeIn);

如果您遍历所有条目并计算每天2次扫描,然后将它们一起添加,您将获得工作人员花费的总时间值。

更新:要遍历数据表,您可以这样,但请注意,它有很多假设,如果可能,您应该更改数据表的结构。我会有一行时间和时间,而不是必须遍历每个条目并找到相应的进入和退出条目。但这取决于您是否可以更改基础数据库。)

所有这一切,在你填满你的DataTable之后 - 我会做类似的事情:

timeSpan.Hours;
timeSpan.Minutes;
timeSpan.Seconds;