我已经找出了两个日期之间的差异......但是 [英] I have worked out the difference between two dates...BUT

查看:78
本文介绍了我已经找出了两个日期之间的差异......但是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在输出文本框中设置一个消息框(以天为单位)



即如果我是0或>你有时间等等,如果你是0或<你没时间离开



我使用的代码如下(但我无法使用if语句)对任何文本框(我得到的)错误或过载消息)



任何建议赞赏

  private   void  dateTimePicker4_ValueChanged( object  sender,EventArgs e)
{
try
{
DateTime startDate =
(DateTime)dateTimePicker4.Value;

DateTime endDate =
(DateTime)dateTimePicker1.Value;

DateTime endDate1 =
(DateTime)dateTimePicker2.Value;
DateTime endDate2 =
(DateTime)dateTimePicker3。值;
TimeSpan ts = endDate.Subtract(startDate);
TimeSpan ts1 = endDate1.Subtract(startDate);
TimeSpan ts2 = endDate2.Subtract(startDate);



textBox1.Text = ts.Days.ToString();
textBox2.Text = ts1.Days.ToString();
textBox3.Text = ts2.Days.ToString();
}
catch
{





从答案1添加:



k对不起(感谢及时回复)



我使用以下内容尝试发布与textbox1.text相关的值

  int  datet; 

datet = int .Parse(textBox1.Text);

if (datet< 17)
{

MessageBox.Show( 时间很短);

}



[/ edit]

解决方案

尝试使用为 System.DateTime定义的' - '减法运算符 type,而不是 System.DateTime.Subtract ,它将使您的代码更具可读性并有助于避免混淆。正如您所知,它产生了类型的返回 System.TimeSpan 。使用此类型查看有多少单位时间。



另外,不要'如果你使用时间,则比较整数。并且,最好不要使用 TextBox 中的输入来操作时间。如果需要,例如,表示天数的整数值(分钟,小时等)添加时间或减去时间,更好地使用 NumericUpDown 控制或类似的东西。



-SA

I would like to set a message box against the output text box (which is in days)

that is if I am "0 or > " you have time left etc and if you are " 0 or <" you have no time left"

the code i have used is as follows (but I am unable to use the if statement) against any of the text boxes(i get an error or overload message)

any advice appreciated

private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate =
                    (DateTime)dateTimePicker4.Value;

                DateTime endDate =
                    (DateTime)dateTimePicker1.Value;

                DateTime endDate1 =
                    (DateTime)dateTimePicker2.Value;
                DateTime endDate2 =
                    (DateTime)dateTimePicker3.Value;
                TimeSpan ts = endDate.Subtract(startDate);
                TimeSpan ts1 = endDate1.Subtract(startDate);
                TimeSpan ts2 = endDate2.Subtract(startDate);



                textBox1.Text = ts.Days.ToString();
                textBox2.Text = ts1.Days.ToString();
                textBox3.Text = ts2.Days.ToString();
            }
            catch
            {



[edit] Added from Answer 1 :

k sorry (and thanks for the prompt response)

I used the following to try and post a value relating to the textbox1.text

int datet;

datet = int.Parse(textBox1.Text);

if (datet <17)
{

MessageBox.Show("time is short);

}


[/edit]

解决方案

Try to use '-' subtraction operator defined for System.DateTime type, instead of System.DateTime.Subtract, it will make your code more readable and help to avoid confusions. As you already know, it produced the return of the type System.TimeSpan. Work with this type to see how many units of time are there.

Also, don't compare integers if you work with time. And, better don't use input from the TextBox to operate with time. If you need, for example, integer value representing days (minutes, hours, etc.) to add to time or subtract from time, better use NumericUpDown control or something similar.

—SA


这篇关于我已经找出了两个日期之间的差异......但是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆