Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need code for getting difference between login and logout time.i used datetimepicker control for selecting date and time but i am getting only days difference but i'm nt able to get the time difference.
I have two tex boxes like starttime and endtime by using datetimepicker i need code for getting time diff in hh:min:sec can any one tell me the code.
Posted

1 solution

This time difference is called Time Span (System.TimeSpan structure) and is calculated, not to much of surprise, using '−' operator. I hardly can understand how could you miss it. It could be like this:
C#
System.DateTime login = System.DateTime.Now;

// remember this point ...

// some time later
System.DateTime logout = System.DateTime.Now;

System.TimeSpan timeSpan = logout - login;
// and so on


I cannot see why would you need a DateTimePicker here, but if you need, it also has the value of System.DateTime. Conceptually, there is no date and time, there is only time. Only you should not work with members of these structures separately. They have everything to calculate what you need.

http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

—SA
 
Share this answer
 
v2

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