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

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

TimeSpan ts = dt1 - dt2;

int hours = ts.Hours;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 5:47am    
Correct, my 5. Also, TotalHours can be used to get a fractional number; "-" operator provides better readability.
--SA
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;
 
Share this answer
 
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;
RaisKazi 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+
RaisKazi 2-Aug-11 8:57am    
Thanks Ravi.
Sergey Alexandrovich Kryukov 3-Aug-11 5:49am    
My 4. Subtraction operator "-" would provide much better readability than Subtract; TotalHours can be used for fractional number.
--SA
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.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900