Click here to Skip to main content
16,020,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my form:
https://scontent-b-hkg.xx.fbcdn.net/hphotos-prn2/t1.0-9/1536719_838907672791325_1190841649_n.jpg

VB
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
Posted
Updated 24-Mar-14 17:55pm
v2

Try this

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

        }
 
Share this answer
 
Comments
akosisugar 25-Mar-14 0:52am    
tNx. just edited your code and now i can continue my project.
Hello,

Considering values of DateTimePicker controls:
C#
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:
C#
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:
C#
// 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 :)
 
Share this answer
 

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