计算2个日期之间的差异并将其存储在数组中

时间:2014-03-20 19:15:44

标签: c# arrays string datetime datepicker

我想计算两个日期之间的差异,一个是从dateTimePicker1中选择的,另一个是2014年2月20日,并将其存储在一个字符串中以添加到我的数组中#34;患者"并能够以另一种形式标签显示它。

到目前为止,我没有错误,但程序没有显示日期之间的差异。 到目前为止,这是我的代码:

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 个答案:

答案 0 :(得分:1)

嗯,你没有任何价值

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.");
    }
}

答案 1 :(得分:0)

尝试显示:

DateTime a=...
DateTime b=...
label.Text=(a - b).TotalDays.ToString();

答案 2 :(得分:0)

在计算日期之间的差异时,人类几乎总是认为它们是整个日期。这意味着您必须将范围视为完全包容

考虑2014-01-012014-01-02的示例。多少天了?你要求的几乎所有人都会说两个。但是,DateTime始终是日期和时间。您可能在午夜指定时间,或者您可能只是忽略时间部分。但它仍然存在。

一旦你有一个时间组件,那么大多数人自然会使结尾部分独占

2014-01-01 00:00:002014-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(如果您将成为使用不变文化并告诉用户以特定格式输入日期。