计算两个日期之间的差异并将其存储在数组中
本文关键字:存储 数组 两个 之间 日期 计算 | 更新日期: 2023-09-27 17:51:06
我想计算两个日期之间的差异,一个选择表单dateTimePicker1和另一个2014年2月20日,并将其存储在字符串中添加到我的数组"Patient"中,并能够在另一个表单标签中显示它。
到目前为止我没有错误,但程序不显示日期之间的差异。这是我到目前为止的代码:
TimeSpan getDateDifference(DateTime date1, DateTime date2)
{
TimeSpan ts = date1 - date2;
int differenceInDays = ts.Days;
string differenceAsString = differenceInDays.ToString();
return ts;
}
public class Patient
{
public string patientidString;
public string firstNameString;
public string lastNameString;
public string dateString;
public string differenceAsString;
public Patient()
{
patientidString = "";
firstNameString = "";
lastNameString = "";
dateString = "";
}
}
//Array
Patient[] patientInfo = new Patient[10];
private void button1_Click(object sender, EventArgs e)
{
TimeSpan difference = getDateDifference(new DateTime(2014, 2, 20), dateTimePicker1.Value);
if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
{
MessageBox.Show(" Patient id, first name and last name cannot be empty");
}
else
try
{
foreach (Patient patientinfoIndex in patientInfo)
patientInfo[itemCountInteger].patientidString = textBox1.Text;
patientInfo[itemCountInteger].firstNameString = textBox2.Text;
patientInfo[itemCountInteger].lastNameString = textBox3.Text;
patientInfo[itemCountInteger].dateString = dateTimePicker1.Text;
string names = patientInfo[itemCountInteger].patientidString + " " + patientInfo[itemCountInteger].firstNameString + " " + patientInfo[itemCountInteger].lastNameString;
listBox1.Items.Add(names);
itemCountInteger++;
listBox1.SelectedItem = names;
}
catch
{
MessageBox.Show("Contacts are limited to 20. Please delete some contacts prior to adding more.");
}
}
//Search Button search a patients name and display his surname in the label if patient is found display his surname
private void button2_Click(object sender, EventArgs e)
{
int intTest = 0;
for (int x = 0; x < patientInfo.Length; x++)
{
if (textBox4.Text == patientInfo[x].patientidString)
{
label6.Text = (patientInfo[x].firstNameString + " " + patientInfo[x].lastNameString);
PatientForm patientform = new PatientForm();
patientform.Show();
patientform.label6.Text = (patientInfo[x].patientidString);
patientform.label7.Text = (patientInfo[x].firstNameString);
patientform.label8.Text =(patientInfo[x].lastNameString);
patientform.dateTimePicker1.Text = (patientInfo[x].dateString);
patientform.label9.Text = (patientInfo[x].differenceAsString);
intTest = 1;
break;
}
}
if (intTest == 0)
{
label6.Text = ("not found");
}
}
你没有给
添加任何值patientInfo[itemCountInteger].differenceAsString;
这就是为什么没有显示,它是一个空的string
。
试着给它一个值:
patientInfo[itemCountInteger].differenceAsString = difference.Days.ToString();
应该是这样的:
private void button1_Click(object sender, EventArgs e)
{
TimeSpan difference = getDateDifference(new DateTime(2014, 2, 20), dateTimePicker1.Value);
if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
{
MessageBox.Show(" Patient id, first name and last name cannot be empty");
}
else
try
{
foreach (Patient patientinfoIndex in patientInfo)
{
patientInfo[itemCountInteger].patientidString = textBox1.Text;
patientInfo[itemCountInteger].firstNameString = textBox2.Text;
patientInfo[itemCountInteger].lastNameString = textBox3.Text;
patientInfo[itemCountInteger].dateString = dateTimePicker1.Text;
patientInfo[itemCountInteger].differenceAsString= difference.Days.ToString();
string names = patientInfo[itemCountInteger].patientidString + " " + patientInfo[itemCountInteger].firstNameString + " " + patientInfo[itemCountInteger].lastNameString;
listBox1.Items.Add(names);
itemCountInteger++;
listBox1.SelectedItem = names;
}
}
catch
{
MessageBox.Show("Contacts are limited to 20. Please delete some contacts prior to adding more.");
}
}
试着显示这个:
DateTime a=...
DateTime b=...
label.Text=(a - b).TotalDays.ToString();
在计算日期之间的差异时,人们几乎总是认为它们是完整的日期。这意味着您必须将该范围视为完全包含。
以2014-01-01
到2014-01-02
为例。一共有多少天?你问的几乎所有人都会说two。但是,DateTime
始终是日期和时间。您可能指定了午夜的时间,或者您可能只是忽略了时间部分。但它还在那里。
一旦你有了一个时间组件,那么大多数人自然会使结束部分排他的。
2014-01-01 00:00:00
到2014-01-02 00:00:00
范围内有多少小时? 24 。
因此,如果您的用户界面只要求日期,并且您使用DateTime
来存储它们,那么不要忘记在差异中添加一天,以说明结束日期将被视为一天的结束,而不是开始。
DateTime startDate = new DateTime(2014,1,1);
DateTime endDate = new DateTime(2014,1,2);
TimeSpan difference = endDate - startDate + TimeSpan.FromDays(1);
int days = (int) difference.TotalDays;
当然,您可以通过验证解析方法之一从UI中获取日期,例如DateTime.TryParse
(如果您的UI是文化感知的)或DateTime.TryParseExact
(如果您打算使用不变文化并告诉用户以特定格式输入日期)。