how to ensure all my datetime is stored as UTC without going through all code? Does this have to be set in db ? how?
thanks.
how to ensure all my datetime is stored as UTC without going through all
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a web app, there are two ways that a non UTC DateTime can get into your database.
1) You did it. Search for all occurrences of DateTime.Now. Those need to be DateTime.UtcNow. Still, a find and replace isn’t going to cover it all, because you’ll need to update every place in your code where you display these dates to your users to convert them to local time.
2) Your user sent you a date/time. There’s no automated way to look for these. You need to manually review every page that the user can use to pass a date/time. Either as data to be saved to your database or as a filter to be used in a query. You’ll need to know their UTC offset and apply it to these occurrences before using them.
Be sure to capture the user’s UTC offset as soon as they enter your site. As of now, the only way to do this is through javascript. If you have a single entry point (ie a login page) then you can put it there. Otherwise, you’ll need to do it in your master page, or, if you don’t have a master page, every page that they could enter on.
to get the UTC Offset in javascript use the following code:
Then pass the offset back to the server. There you can create two functions to handle converting from/to UTC.
Where UtcOffset holds the javascript Offset: