Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

Please look more thoroughly at the structure System.DateTime. Is has a subtraction operator defined. So:

C#
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


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. :-)

—SA
 
Share this answer
 
I think it may help you...
C#
MsgBox(DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value))
MsgBox(DateDiff(DateInterval.Month, DateTimePicker1.Value, DateTimePicker2.Value))
MsgBox(DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTimePicker2.Value))
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900