2

I am using ASP.NET MVC 4 for my web application and all database access is through WCF service. Now I want to display all the dates in my system in users timezone. The user timezone is saved in database and on login, I am saving it in the cookie. What I am doing is in Controller I am calling a utility method and converting all datetime to user's timezone. Is this the right way to do? Or should I call the utility method directly in the view? Please advice the best way to handle the situation.

2 Answers 2

2

Ideally, your utility method should return the time in user’s time zone (after passing it an argument that helps you determine the user’s time zone). You should then call your utility method either from a controller action or from the business layer (or from wherever you are populating your view model), then use the information in your view model to render your view.

Sign up to request clarification or add additional context in comments.

Comments

-1

Always store UTC dates in the database as you do not know who is going to view the items.

Before storing

var dbDate = myDate.ToUniversalTime();

And when viewing:

var userDate = dbDate.ToLocalTime();

Do remember to convert any dates to UTC before making SQL queries etc.

MSDN: http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.