C# 计算两个日期之间的差异并将其存储在数组中
我想计算两个日期之间的差异,一个是拾取的表单dateTimePicker1,另一个是2014年2月20日,并将其存储在字符串中,添加到我的数组“Patient”中,并能够在另一个表单标签中显示它 到目前为止,我没有错误,但程序没有显示日期之间的差异。 这是我目前的代码:C# 计算两个日期之间的差异并将其存储在数组中,c#,arrays,string,datetime,datepicker,C#,Arrays,String,Datetime,Datepicker,我想计算两个日期之间的差异,一个是拾取的表单dateTimePicker1,另一个是2014年2月20日,并将其存储在字符串中,添加到我的数组“Patient”中,并能够在另一个表单标签中显示它 到目前为止,我没有错误,但程序没有显示日期之间的差异。 这是我目前的代码: TimeSpan getDateDifference(DateTime date1, DateTime date2) { TimeSpan ts = date1 - date2; int differenceIn
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");
}
}
时间跨度getDateDifference(日期时间日期1、日期时间日期2)
{
TimeSpan ts=日期1-日期2;
整数差日=ts.天;
字符串differenceString=differenceInDays.ToString();
返回ts;
}
公立病人
{
公共字符串;
公共字符串firstNameString;
公共字符串lastNameString;
公共字符串日期字符串;
公共字符串;
公共病人()
{
patientidString=“”;
firstNameString=“”;
lastNameString=“”;
日期字符串=”;
}
}
//排列
患者[]患者信息=新患者[10];
私有无效按钮1\u单击(对象发送者,事件参数e)
{
TimeSpan差异=getDateDifference(新的日期时间(2014,2,20),dateTimePicker1.Value);
如果(textBox1.Text.Length==0 | | textBox2.Text.Length==0 | | | textBox3.Text.Length==0)
{
MessageBox.Show(“患者id、名字和姓氏不能为空”);
}
其他的
尝试
{
foreach(患者信息中的患者信息索引)
patientInfo[itemCountInteger].patientidString=textBox1.Text;
patientInfo[itemCountInteger].firstNameString=textBox2.Text;
patientInfo[itemCountInteger].lastNameString=textBox3.Text;
patientInfo[itemCountInteger].dateString=dateTimePicker1.Text;
字符串名称=patientInfo[itemCountInteger]。PatientString+“”+patientInfo[itemCountInteger]。firstNameString+“”+patientInfo[itemCountInteger]。lastNameString;
listBox1.Items.Add(名称);
itemCountInteger++;
listBox1.SelectedItem=名称;
}
抓住
{
MessageBox.Show(“联系人限制为20个,请在添加更多联系人之前删除一些联系人”);
}
}
//搜索按钮搜索患者姓名并在标签中显示其姓氏如果找到患者显示其姓氏
私有无效按钮2\u单击(对象发送者,事件参数e)
{
int检验=0;
for(int x=0;xDateTime a=...
DateTime b=...
label.Text=(a - b).TotalDays.ToString();
你没有把任何价值放在
patientInfo[itemCountInteger].differenceAsString;
这就是为什么没有显示任何内容,它是一个空的字符串
试着给它一个值:
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.");
}
}
当计算日期之间的差异时,人类几乎总是认为它们是完整的日期。这意味着您必须将范围视为完全包含
考虑示例2014-01-01
到2014-01-02
。有多少天?几乎你问的任何人都会说两个。但是,DateTime
始终是日期和时间。您可能是在午夜指定时间,也可能只是忽略时间部分。但它仍然在那里
一旦你有了时间成分,那么大多数人自然会把结尾部分排他
在2014-01-01 00:00:00
到2014-01-02 00:00:00
的范围内有多少小时?正好24
因此,如果您的用户界面只要求输入日期,而您使用的是DateTime
来存储日期,那么不要忘记在差异中添加一天,以说明将结束日期视为一天的结束,而不是一天的开始
当然,您可以通过其中一种验证解析方法从UI获取日期,例如DateTime.TryParse
(如果您的UI具有区域性意识)或DateTime.TryParseExact
(如果您要使用不变区域性并告诉用户以特定格式输入日期)。欢迎回来!首先,在数组中存储一个值和向用户显示一个值是有区别的。我如何将日期的差异添加到字符串中检查我的答案,首先,difference
的类型是TimeSpan
,因此,例如,如果要以天为单位显示差异,可以说difference.days.ToString()
,你必须在这里说ToString()
,因为Days
是一个整数值。对不起,你能记下代码吗,因为我需要修改我的程序original@user3439080-StackOverflow的所有贡献均获得知识共享许可。请参见每页页脚处的注释。这意味着,一旦有人在这里发布任何代码,所有人都可以免费使用。只有在极端情况下,如果有人发布了已经受版权保护的内容,才会进行删除。然后,您必须遵循中描述的删除通知流程。@PaulG您至少可以更改变量的名称吗
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;