Click here to Skip to main content
14,638,959 members
Rate this:
Please Sign up or sign in to vote.
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.
neaS
Posted
Comments
Suresh Suthar 10-Aug-11 6:06am
   
Post your code. This will help us to help you.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:

Timespan.TotalHours[^]
Timespan.Hours[^]

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

:)
   
Comments
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 :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

Doesn't this works?

int hours = (dateTimePicker1.Value - dateTimePicker2.Value).Hours
   
Comments
neeraj24may 10-Aug-11 6:41am
   
no,it show an error"int does not contain a defination for hours"
lw@zi 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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100