【问题标题】:Calculating the difference between 2 dates and storing it in an array计算两个日期之间的差异并将其存储在数组中
【发布时间】:2014-03-20 19:15:43
【问题描述】:

我想计算两个日期之间的差异,一个是 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");
    }
}

【问题讨论】:

  • 欢迎回来!首先,在数组中存储一个值和向用户显示一个值是有区别的。

标签: c# arrays string datetime datepicker


【解决方案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.");
    }
}

【讨论】:

  • 如何将日期差异添加到该字符串
  • 检查我的答案,首先,difference 的类型是 TimeSpan,所以例如如果你想显示天数的差异,说difference.Days.ToString(),你必须说@987654329 @ 这里是因为 Days 是一个 int 值。
  • 对不起,你能把代码删掉吗,因为我需要我的程序是原创的
  • @user3439080 - 所有对 StackOverflow 的贡献均获得知识共享许可。请参阅每页页脚的注释。这意味着一旦有人在这里发布任何代码,它就可供所有人免费使用。如果有人发布了已经受版权保护的东西,那么删除只是在极端情况下完成的事情。然后,您必须遵循terms of service 中描述的删除通知流程。
  • @PaulG 至少可以更改变量的名称
【解决方案2】:

尝试显示这个:

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

【讨论】:

    【解决方案3】:

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

    考虑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(如果您要使用不变的文化并告诉您的用户以特定格式输入日期)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2013-02-01
      • 1970-01-01
      • 2018-11-29
      • 2021-11-25
      相关资源
      最近更新 更多