I want to calculate the differences between two dates, one picked form dateTimePicker1 and the other one 20 February of 2014 and store it in a string to added to my array "Patient" and be able to display it in another form label.
我想计算两个日期之间的差异,一个是从dateTimePicker1中选择的,另一个是2014年2月20日,并将其存储在一个字符串中,以添加到我的数组“Patient”中,并能够在另一个表单标签中显示它。
So far I have no errors but the program doesn't display the difference between dates. This is my code so far:
到目前为止,我没有错误,但程序没有显示日期之间的差异。到目前为止这是我的代码:
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");
}
}
3 个解决方案
#1
1
Well you're not putting any value into
那么你没有任何价值
patientInfo[itemCountInteger].differenceAsString;
This is why nothing is showing, it's an empty string
.
这就是为什么没有显示,它是一个空字符串。
Try to give it a value:
试着给它一个值:
patientInfo[itemCountInteger].differenceAsString = difference.Days.ToString();
This is how it should look:
它应该是这样的:
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.");
}
}
#2
0
Try displaying this:
尝试显示:
DateTime a=...
DateTime b=...
label.Text=(a - b).TotalDays.ToString();
#3
0
When calculating the difference between dates, human beings almost always consider them to be whole dates. That means you must treat the range as being fully inclusive.
在计算日期之间的差异时,人类几乎总是将它们视为整个日期。这意味着您必须将范围视为完全包容。
Consider the example 2014-01-01
to 2014-01-02
. How many days are there? Almost any human you ask will say two. However, a DateTime
is always a date and a time. You might be specifying the time at midnight, or you might just be ignoring the time portion. But it's still there.
请考虑2014-01-01到2014-01-02的示例。有几天?几乎所有你问的人都会说两个。但是,DateTime始终是日期和时间。您可能在午夜指定时间,或者您可能只是忽略时间部分。但它仍然存在。
Once you have a time component, then most humans will naturally make the ending part exclusive.
一旦你有了时间成分,那么大多数人自然会将结尾部分排除在外。
How many hours in the range 2014-01-01 00:00:00
to 2014-01-02 00:00:00
? Exactly 24.
2014-01-01 00:00:00到2014-01-02 00:00:00在几个小时的范围内?正好24。
Therefore, if your user interface is only asking for dates, and you're using a DateTime
to store them in, then don't forget to add a day to the difference to account for the ending date to be treated as the end of the day, rather than the start of it.
因此,如果您的用户界面仅询问日期,并且您正在使用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;
Of course, you can get a date from your UI via one of the validating parsing methods, such as DateTime.TryParse
(if your UI is culture aware) or DateTime.TryParseExact
(if you are going to be using the invariant culture and telling your user to enter the date in a specific format).
当然,您可以通过一种验证解析方法从UI获取日期,例如DateTime.TryParse(如果您的UI具有文化感知)或DateTime.TryParseExact(如果您将使用不变文化并告诉您用户以特定格式输入日期)。
#1
1
Well you're not putting any value into
那么你没有任何价值
patientInfo[itemCountInteger].differenceAsString;
This is why nothing is showing, it's an empty string
.
这就是为什么没有显示,它是一个空字符串。
Try to give it a value:
试着给它一个值:
patientInfo[itemCountInteger].differenceAsString = difference.Days.ToString();
This is how it should look:
它应该是这样的:
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.");
}
}
#2
0
Try displaying this:
尝试显示:
DateTime a=...
DateTime b=...
label.Text=(a - b).TotalDays.ToString();
#3
0
When calculating the difference between dates, human beings almost always consider them to be whole dates. That means you must treat the range as being fully inclusive.
在计算日期之间的差异时,人类几乎总是将它们视为整个日期。这意味着您必须将范围视为完全包容。
Consider the example 2014-01-01
to 2014-01-02
. How many days are there? Almost any human you ask will say two. However, a DateTime
is always a date and a time. You might be specifying the time at midnight, or you might just be ignoring the time portion. But it's still there.
请考虑2014-01-01到2014-01-02的示例。有几天?几乎所有你问的人都会说两个。但是,DateTime始终是日期和时间。您可能在午夜指定时间,或者您可能只是忽略时间部分。但它仍然存在。
Once you have a time component, then most humans will naturally make the ending part exclusive.
一旦你有了时间成分,那么大多数人自然会将结尾部分排除在外。
How many hours in the range 2014-01-01 00:00:00
to 2014-01-02 00:00:00
? Exactly 24.
2014-01-01 00:00:00到2014-01-02 00:00:00在几个小时的范围内?正好24。
Therefore, if your user interface is only asking for dates, and you're using a DateTime
to store them in, then don't forget to add a day to the difference to account for the ending date to be treated as the end of the day, rather than the start of it.
因此,如果您的用户界面仅询问日期,并且您正在使用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;
Of course, you can get a date from your UI via one of the validating parsing methods, such as DateTime.TryParse
(if your UI is culture aware) or DateTime.TryParseExact
(if you are going to be using the invariant culture and telling your user to enter the date in a specific format).
当然,您可以通过一种验证解析方法从UI获取日期,例如DateTime.TryParse(如果您的UI具有文化感知)或DateTime.TryParseExact(如果您将使用不变文化并告诉您用户以特定格式输入日期)。