locked
Getting Time difference in Negative values (trying to get session time for my application manually) RRS feed

  • Question

  • User718865757 posted

    Hi,

    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.

    starttime = DateTime.UtcNow.TimeOfDay;
    
    TimeSpan FirstTime = Convert.ToDateTime(starttime).TimeOfDay;
    TimeSpan currentTime = DateTime.UtcNow.TimeOfDay;
    
    TimeSpan finalsession = currentTime.Subtract(FirstTime);

    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.

    Thursday, March 19, 2015 9:34 AM

Answers

  • User718865757 posted

    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 :

    TimeSpan finalsession = (DateTime.UtcNow - start).Duration();

    Thanks a lot i will try this...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 19, 2015 10:19 AM
  • User281315223 posted

    i used Duration() also... still getting negative values..

    Duration should always be non-negative. Could you post the code of how you are current using it?

    // Output your dates
    DateTime.UtcNow - DateTime.UtcNow.AddDays(2)).ToString();             // -2.00:00:00
    DateTime.UtcNow - DateTime.UtcNow.AddDays(2)).Duration().ToString();  //  2.00:00:00
    

    You can see an example here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 20, 2015 11:18 AM

All replies

  • User281315223 posted

    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 :

    TimeSpan finalsession = (DateTime.UtcNow - start).Duration();

    Thursday, March 19, 2015 9:40 AM
  • User718865757 posted

    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 :

    TimeSpan finalsession = (DateTime.UtcNow - start).Duration();

    Thanks a lot i will try this...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 19, 2015 10:19 AM
  • User718865757 posted

    Hi 

    Rion William...

    I tried your code ... i think your code is working well.  Did not get negative values yet.

    Thanks for your help.

    Friday, March 20, 2015 11:00 AM
  • User281315223 posted

    i used Duration() also... still getting negative values..

    Duration should always be non-negative. Could you post the code of how you are current using it?

    // Output your dates
    DateTime.UtcNow - DateTime.UtcNow.AddDays(2)).ToString();             // -2.00:00:00
    DateTime.UtcNow - DateTime.UtcNow.AddDays(2)).Duration().ToString();  //  2.00:00:00
    

    You can see an example here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 20, 2015 11:18 AM
  • User718865757 posted

    shivanag

    i used Duration() also... still getting negative values..

    Duration should always be non-negative. Could you post the code of how you are current using it?

    // Output your dates
    DateTime.UtcNow - DateTime.UtcNow.AddDays(2)).ToString();             // -2.00:00:00
    DateTime.UtcNow - DateTime.UtcNow.AddDays(2)).Duration().ToString();  //  2.00:00:00
    

    You can see an example here.

    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.

    Friday, March 20, 2015 11:25 AM