Im getting time difference by using the following code
//Here starttime im passing from login page (capturing when user logins), i.e starttime = DateTime.TimeOfDay;
TimeSpan FirstTime = Convert.ToDateTime(starttime).TimeOfDay;
TimeSpan currentTime = DateTime.TimeOfDay;
TimeSpan finalsession = currentTime.Subtract(FirstTime);
The scenario is my server is in USA ,so when the user uses my application from different timezone (other than USA timezone) we are getting negative values like -9:-03:-33.22
Then after to fix this negative values i used UtcNow (global time). Now most of the time not getting negative values everything is ok.
You'll likely want to be consistent when you are storing your dates and when you are comparing them. So if you are using the DateTime.UtcNow property to compare your dates to, you will want to use the same property to store your current DateTime.
Additionaly you shouldn't need to use TimeOfDay, you will be able to easily compare your DateTime objects on their own :
// Example Setting your Start Time
DateTime start = DateTime.UtcNow;
// When you need to calculate your difference, just subtract your previous time from now
TimeSpan finalsession = DateTime.UtcNow - start;
I suppose that since you were previously using non-UTC values and comparing them to UTC values that the difference could be large enough to generate negative values. If you were using the TimeOfDay properties as well, I suppose this could have possibly used
a different date and resulted in negative values as well.
One final note is that if you want to ensure that you aren't receiving any negative values, you can use
the TimeSpan.Duration() method which will return a non-negative value for your TimeSpan :
You'll likely want to be consistent when you are storing your dates and when you are comparing them. So if you are using the DateTime.UtcNow property to compare your dates to, you will want to use the same property to store your current DateTime.
Additionaly you shouldn't need to use TimeOfDay, you will be able to easily compare your DateTime objects on their own :
// Example Setting your Start Time
DateTime start = DateTime.UtcNow;
// When you need to calculate your difference, just subtract your previous time from now
TimeSpan finalsession = DateTime.UtcNow - start;
I suppose that since you were previously using non-UTC values and comparing them to UTC values that the difference could be large enough to generate negative values. If you were using the TimeOfDay properties as well, I suppose this could have possibly used
a different date and resulted in negative values as well.
One final note is that if you want to ensure that you aren't receiving any negative values, you can use
the TimeSpan.Duration() method which will return a non-negative value for your TimeSpan :
Thanks Rion, sorry i updated the previous message. may be that negative values came before i update my build. i will check tomorrow also. So that i will be clear.
Member
6 Points
43 Posts
Getting Time difference in Negative values (trying to get session time for my application manuall...
Mar 19, 2015 09:34 AM|shivanag|LINK
Hi,
Im getting time difference by using the following code
The scenario is my server is in USA , so when the user uses my application from different timezone (other than USA timezone) we are getting negative values like -9:-03:-33.22
Then after to fix this negative values i used UtcNow (global time). Now most of the time not getting negative values everything is ok.
But rarely for some users getting Negative values again after using UtcNow also. Im unable to track this how it is happening?
Any one please tell me whats going wrong here (or) please suggest better way to find the time difference.
THANKS,
Shivanag.
All-Star
114593 Points
18503 Posts
MVP
Re: Getting Time difference in Negative values (trying to get session time for my application man...
Mar 19, 2015 09:40 AM|Rion Williams|LINK
You'll likely want to be consistent when you are storing your dates and when you are comparing them. So if you are using the DateTime.UtcNow property to compare your dates to, you will want to use the same property to store your current DateTime.
Additionaly you shouldn't need to use TimeOfDay, you will be able to easily compare your DateTime objects on their own :
I suppose that since you were previously using non-UTC values and comparing them to UTC values that the difference could be large enough to generate negative values. If you were using the TimeOfDay properties as well, I suppose this could have possibly used a different date and resulted in negative values as well.
One final note is that if you want to ensure that you aren't receiving any negative values, you can use the TimeSpan.Duration() method which will return a non-negative value for your TimeSpan :
Member
6 Points
43 Posts
Re: Getting Time difference in Negative values (trying to get session time for my application man...
Mar 19, 2015 10:19 AM|shivanag|LINK
Thanks a lot i will try this...
Member
6 Points
43 Posts
Re: Getting Time difference in Negative values (trying to get session time for my application man...
Mar 20, 2015 11:00 AM|shivanag|LINK
Hi
Rion William...
I tried your code ... i think your code is working well. Did not get negative values yet.
Thanks for your help.
All-Star
114593 Points
18503 Posts
MVP
Re: Getting Time difference in Negative values (trying to get session time for my application man...
Mar 20, 2015 11:18 AM|Rion Williams|LINK
Duration should always be non-negative. Could you post the code of how you are current using it?
You can see an example here.
Member
6 Points
43 Posts
Re: Getting Time difference in Negative values (trying to get session time for my application man...
Mar 20, 2015 11:25 AM|shivanag|LINK
Thanks Rion, sorry i updated the previous message. may be that negative values came before i update my build. i will check tomorrow also. So that i will be clear.
Again thanks for your quick response.