Click here to Skip to main content
15,748,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i want to calculate hours difference between two datetime pickers. but difference always comes out to be zero.
what will i do.
Suresh Suthar 10-Aug-11 6:06am    
Post your code. This will help us to help you.

might be helpful,

int hours = (dateTimePicker1.Value - dateTimePicker2.Value).TotalHours

There is implementation difference between TotalHours and Hours,

TotalHours implementation is something like below,

return (this._ticks * 2.7777777777777777E-11);

Hours implementation is something like below,

return (int) ((this._ticks / 0x861c46800L) % 0x18L);

Future Ref:


and .Net Reflector/[^] is the best help.

Share this answer
BobJanova 10-Aug-11 8:21am    
Correct, 5. Although the OP may well want to do 'picker2 - picker1' if it is a range with the beginning specified first.
Mohammad A Rahman 10-Aug-11 17:18pm    
Thanks Bob :)
Doesn't this works?

int hours = (dateTimePicker1.Value - dateTimePicker2.Value).Hours
Share this answer
neeraj24may 10-Aug-11 6:41am    
no,it show an error"int does not contain a defination for hours"
dan!sh 10-Aug-11 6:44am    
You may have something wrong there. Cna you post your code?
#realJSOP 10-Aug-11 7:40am    
You have to use TotalHours to account for the possiblity of more than 0 days in the resulting timespan object.

Reiner Lim 29-Nov-12 3:52am    
string hours = (dateTimePicker2.Value - dateTimePicker1.Value).TotalHours.ToString();

textBox1.Text = hours;

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