如何使用vb.net自动计算天数 [英] How to auto calculate number of days using vb.net

查看:285
本文介绍了如何使用vb.net自动计算天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格:

https://scontent-b-hkg.xx.fbcdn.net/hphotos-prn2/t1.0-9/1536719_838907672791325_1190841649_n.jpg



< pre lang =vb> DueDateTimePicker.Value = DateTime.Now.AddDays(txtNumOfDays.Text)


$ t $ b以上代码在txtNumOfDays文本更改期间有效。这将设置duedate开始从发布日期开始。



我想要的是在DueDateTimePicker值更改期间,txtNumOfDays将在发布日期和截止日期之间自动计算。例如,今天的日期是25用户设置截止日期为26然后答案的天数是1.请帮助仍然是newbiew

解决方案

试试这个



  private   void  dateTimePicker2_ValueChanged( object  sender,EventArgs e)
{
DateTime arrivaldate = dateTimePicker1.Value;
DateTime departuredate = dateTimePicker2.Value;
double DaysStayed = departuredate.Subtract(arrivaldate).TotalDays;

}


你好,



考虑DateTimePicker控件的值:

 dtpDueDate.Value = 2014-03-25  15  39  12  
dtpIssuedDate.Value = 2014-03-27 14 11 15





提取两个DateTime对象将为您提供TimeSpan结构的结果。如果您的日期对象包含时间部分,您将获得两个日期之间的确切差异。在这种情况下,它将是:

1.22:32:03(1天,22小时,32分钟和3秒)



解决方案1:

 TimeSpan timeSpan = dtpDueDate.Value  -  dtpIssuedDate.Value; 
// 1.此代码的结果如下:1(仅限天数)
txtNumOfDays.Text = timeSpan.Days.ToString();
// 2.此代码的结果如下:1,93892361111111
txtNumOfDays.Text = timeSpan.TotalDays.ToString();





解决方案2:

如果你愿意在没有比较TimePart使用DateTime.Date的情况下只得到天差异:

  //   dtpDueDate:2014-03-25 00:00:00  
// dtpIssuedDate:2014-03-26 00:00:00
TimeSpan timeSpan2 = dtpDueDate.Value.Date - dtpIssuedDate.Value.Date;
// 1.此代码将产生如下结果:2
txtNumOfDays .Text = timeSpan2.TotalDays.ToString();





TimeSpan信息:

http://msdn.microsoft.com/library/system.timespan%28v=vs.110%29 .aspx [ ^ ]

日期时间信息:

http ://msdn.microsoft.com/library/system.datetime.aspx [ ^ ]



我希望它可以帮助你:)


my form:
https://scontent-b-hkg.xx.fbcdn.net/hphotos-prn2/t1.0-9/1536719_838907672791325_1190841649_n.jpg

DueDateTimePicker.Value = DateTime.Now.AddDays(txtNumOfDays.Text)


above code works during txtNumOfDays text changed.this will set the duedate starting from issued date.

what i want is during DueDateTimePicker value changed, txtNumOfDays will auto calculate between issued date and due date. For example, todays date is 25 user set the due date to 26 then number of days answer is 1.Please help still a newbiew

解决方案

Try this

 private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            DateTime arrivaldate  = dateTimePicker1.Value;
DateTime departuredate = dateTimePicker2.Value;
            double DaysStayed = departuredate.Subtract(arrivaldate).TotalDays;

        }


Hello,

Considering values of DateTimePicker controls:

dtpDueDate.Value = 2014-03-25 15:39:12
dtpIssuedDate.Value = 2014-03-27 14:11:15



Substracting two DateTime objects will give you result in TimeSpan struct. If your date objects contains Time part you will get exact difference between two dates. In this case it will be:
1.22:32:03 (1 day, 22 hours, 32 minutes and 3 seconds)

Solution 1:

TimeSpan timeSpan = dtpDueDate.Value - dtpIssuedDate.Value;
// 1. This code will result something like this: 1 (only days)
txtNumOfDays.Text = timeSpan.Days.ToString();
// 2. This code will result something like this: 1,93892361111111
txtNumOfDays.Text = timeSpan.TotalDays.ToString();



Solution 2:
If you wish to get only days difference without comparing TimePart use DateTime.Date:

// dtpDueDate: 2014-03-25 00:00:00
// dtpIssuedDate: 2014-03-26 00:00:00
TimeSpan timeSpan2 = dtpDueDate.Value.Date - dtpIssuedDate.Value.Date;
// 1. This code will result something like this: 2
txtNumOfDays.Text = timeSpan2.TotalDays.ToString();



TimeSpan info:
http://msdn.microsoft.com/library/system.timespan%28v=vs.110%29.aspx[^]
DateTime info:
http://msdn.microsoft.com/library/system.datetime.aspx[^]

I hope it helps you :)


这篇关于如何使用vb.net自动计算天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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