日期时间减法
本文关键字:时间 日期 | 更新日期: 2023-09-27 18:34:51
我的注册程序在人们扫描自己时会使用他们的姓名、ID 和"Time In:"登录并隐藏他们,但在他们的名字中添加"Time Out:",反之亦然。我想做的是每次这个人扫描自己"进入"时,我希望它查看时间"进入"和"输出"并计算在办公室的总时间。
附上代码:
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;
}
您可以使用以下命令获取两个 DateTime 对象之间的差异:
TimeSpan timePassed = timeOut.Subtract(timeIn);
其中timeOut
和timeIn
是DateTime
对象。
如果您需要将差异(或任一时间(显示为字符串,我建议您仅在进行所需的计算后将它们转换为字符串。尽可能始终使用强类型对象。