Reputation: 87
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.
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");
}
}
Upvotes: 0
Views: 205
Reputation: 241718
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.
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.
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 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).
Upvotes: 0
Reputation: 7122
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.");
}
}
Upvotes: 1
Reputation: 3915
Try displaying this:
DateTime a=...
DateTime b=...
label.Text=(a - b).TotalDays.ToString();
Upvotes: 0