简体   繁体   中英

DateTime Subtraction

My registration program logs people in with their name, id and "Time In:" when they scan themselves in, and hides them, but adds "Time Out: " to their name, and vice versa. What im looking to do is every time the person scans themselves "In" I want it to look at the Time "In"'s amd "out"'s and calculate thr total time IN the office.

Attached is the code :

    private string CreateTimeEntry(string current)
    {
        var indexIn = current.LastIndexOf("Time In : "); // Get the last index of the word "in"
        var indexOut = current.LastIndexOf("Time Out : "); // Get the last index of the word out
        string timeIn = current + "      " + "Time In : ";
        string timeOut = current + "      " + "Time Out : ";

        if (indexOut > indexIn)
        {
            // if the last "out" comes after the last "in"
            return timeIn;
        }
        else
        {
           // If the last "in" comes after the last "out"
            return timeOut;
        }
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        Object returnValue;

        string txtend = textBox1.Text;

        if (e.KeyChar == 'L')
        {   
            DBConnection.Open();
        }
        if (DBConnection.State == ConnectionState.Open)
        {
            if (textBox1.Text.Length != 6) return;
            {
                cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
                cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
                cmd.CommandType = CommandType.Text;
                cmd.Connection = DBConnection;

                returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")";

                DBConnection.Close();

                bool found = false;

                System.DateTime resultTime1 = new System.DateTime(;

                foreach (var item in listBox1.Items)
                {
                    var itemEntry = item.ToString();

                    string newEntry = CreateTimeEntry(itemEntry) + DateTime.Now.ToString("HH:mm") + "   " + "Total Time: " + resultTime1 ;

                    if (itemEntry.Contains(returnValue.ToString()))
                    {
                        var indexIn = itemEntry.LastIndexOf("Time In : ");
                        var indexOut = itemEntry.LastIndexOf("Time Out : ");

                        if (indexOut > indexIn)
                        {
                            listBox2.Items.Remove(item);
                            listBox1.Items.Add(newEntry);
                            found = true;
                            break;
                        }
                        else
                        {
                            listBox1.Items.Remove(item);
                            listBox2.Items.Add(newEntry);
                            found = true;
                            break;
                        }

                    }

                }

                if (!found)
                {
                    string newEntry2 = "";
                    foreach (string str in listBox2.Items)
                    {
                        var itemEntry2 = str;
                        newEntry2 = CreateTimeEntry(itemEntry2) + DateTime.Now.ToString("HH:mm");

                        //if (listBox2.Items.Contains(returnValue.ToString()))
                        if (listBox2.Items.Contains(str) && str.Contains(textBox1.Text))
                        {
                            var indexIn = itemEntry2.LastIndexOf("Time In : ");
                            var indexOut = itemEntry2.LastIndexOf("Time Out : ");

                            if (indexOut > indexIn)
                            {
                                listBox2.Items.Remove(str);
                                listBox1.Items.Add(newEntry2);
                                found = true;
                                break;
                            }
                        }
                    }
                    var itemEntry = listBox1.Items.ToString();
                    var itemEntry1 = listBox2.Items.ToString();

                    if (!listBox1.Items.Contains(newEntry2))
                    {
                        listBox1.Items.Add(returnValue + "      " + "Time In : " + DateTime.Now.ToString("HH:mm"));
                    }
                }
            }

            textBox1.Clear();

            System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName);
            foreach (object item1 in listBox1.Items)
                SaveFile.WriteLine(item1.ToString());
            SaveFile.Flush();
            SaveFile.Close();

            if (listBox1.Items.Count != 0) { DisableCloseButton(); }
            else
            {
                EnableCloseButton();
            }
            Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
            e.Handled = true;
        }

You can get the difference between two DateTime objects using the following:

TimeSpan timePassed = timeOut.Subtract(timeIn);

where timeOut and timeIn are DateTime objects.

If you need the difference (or either of the times) displayed somewhere as a string, I would recommend converting them to strings only after doing the calculations that you need. Always work with strongly typed objects when possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM