Click here to Skip to main content
14,636,049 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends,
I m bulding a window application "LEAVE MANAGEMENT SYSTEM",In this i have take two date time picker.i bulid this leave management system for industry where working hours is 8.i want to calculate "working hours of leave using date time picker"..
pls help.
i dnt know how to take diff from two date time picker and i user select 1 day leave than in working hours day should be 8 hours..
Thanks in advance.
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

To get the difference between two date time values, use the Timespan class.
DateTime dt1 = firstdate;
DateTime dt2 = seconddate;

TimeSpan ts = dt1 - dt2;

int hours = ts.Hours;
   
Comments
   
Correct, my 5. Also, TotalHours can be used to get a fractional number; "-" operator provides better readability.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try as below code.

DateTime startTime = Convert.ToDateTime(DateTimePicker1.value); 
DateTime endTime = Convert.ToDateTime(DateTimePicker2.value); 

TimeSpan span = endTime.Subtract(startTime); 

int hoursDifference = span.Hours;
   
Comments
advancedansh 2-Aug-11 2:02am
   
thanks but would u like to tell me if i do this then user select two diff date in dtp but same time let sy 12 o'clock then what happen it only give same days difference and hours diff = 0;
[no name] 2-Aug-11 3:10am
   
Then try as below.

DateTime startTime = Convert.ToDateTime(DateTimePicker1.value);
DateTime endTime = Convert.ToDateTime(DateTimePicker2.value);

TimeSpan span = endTime.Subtract(startTime);

int daysDiff = span.Days;
int hoursDiff = span.Hours;

hoursDiff = hoursDiff + ( daysDiff * 24);
RaviRanjanKr 2-Aug-11 8:28am
   
My 5+
[no name] 2-Aug-11 8:57am
   
Thanks Ravi.
   
My 4. Subtraction operator "-" would provide much better readability than Subtract; TotalHours can be used for fractional number.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

DateTime startTime = Convert.ToDateTime(_dtpOutageStart.Value);
DateTime endTime = Convert.ToDateTime(_dtpOutageComplte.Value);

TimeSpan span = endTime.Subtract(startTime);


_txtTotalOutage.Text = span.ToString();

result =day:hour:minitues

actually i wan day=hour+hour:minute

for example 1 day 24 hour and 2hour 25 minutes total 26 hour so I need 26 hour:25 minutes is any body help me.
   
Comments
BillWoodruff 13-Nov-14 14:40pm
   
You are posting a new question on a thread over two years old.

You should post a new question, and remove this one.

And, yes, it's pretty easy to compute what I think you want.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100