问题描述
我在Visual Studio 2008中使用VB.有人知道要减去2 datetimepicker确定VB中的天数的代码是什么?我正在为一个项目做项目,因此我必须为一个特定的任务计算多少天,这一点至关重要.我需要用一个空文本框填充.天.
在此先感谢.
Hi,
I''m using VB in visual studio 2008. Anyone know what are the codes to subtracting 2 datetimepickers to determine the number of days in VB? i''m doing a project for and it is essential that i count how many days for a specific task. I need to fill an empty textbox with the no. of days.
Thanks in advance.
推荐答案
System.DateTime first = firstDateTimePicker.Value;
System.DateTime second = secondDateTimePicker.Value;
//best syntax is using operator, not the method call, because it looks unambiguous:
System.TimeSpan timeSpan = second - time; //not a problem if first is later time, negative time spans are allowed
请参阅:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value.aspx [ ^ ].
现在,更彻底地查看结构System.TimeSpan
:
http://msdn.microsoft.com/en-us/library/system.timespan.aspx [ ^ ].
它具有一切以所需单位表示垃圾邮件时间的功能.请注意,Total…
属性为您提供任何单位的分数值;其他属性可根据需要计算整数天数,小时数等.
仔细阅读MSDN;你会没事的. :-)
Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value.aspx[^].
Now, look more thoroughly at the structure System.TimeSpan
:
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].
It has everything to express the time spam in required units. Pay attention that Total…
properties give you the fractional value in any units; other properties calculate integer number of days, hours, etc., whatever you need.
Read MSDN more thoroughly; and you will be all right. :-)
MsgBox(DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value))
MsgBox(DateDiff(DateInterval.Month, DateTimePicker1.Value, DateTimePicker2.Value))
MsgBox(DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTimePicker2.Value))
这篇关于如何从两个datetimepicker中减去日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!