R
R
Ruslan2016-11-15 20:30:02
ASP.NET
Ruslan, 2016-11-15 20:30:02

How to take into account the user's timezone on an asp.net site?

Hello!
There is an old and big project on asp.net mvc. Hundreds of controllers and views used the @DateTime.Now construct to determine or display the current time on the page, which, of course, outputs the server's current time. Now I want to take into account the user's time zone (which is recorded in his profile).
I see a long and laborious path to this goal (which scares me with its size):
- in all places where I write the current date to the database, use DateTime.UtcNow
- in all places where I display the current date of the user, transfer it from world to local time
- all old dates in all database records should be converted from local time to world time
- the logic of services should be rewritten taking into account the time that is logical for it
in general, you will have to delve deeply into each line of code and, most likely, there will be errors %(
Maybe there are approaches that will make the task easier? for example, somehow overload the DateTime.Now method so that it returns world time or somehow set up the context ( session) of the user so that his local (other than the server) time is there.
Thank you for your attention and advice

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2016-11-15
@AlekseyNemiro

DateTime.Now - just do a search and replace (by files). You can limit your search to file types ( .cs , .cshtml , .aspx , .ascx ) to keep things simple.
As for displaying the time in the desired time zone, it might be worth doing it using JavaScript . For each individual user, displaying time from the server side will be expensive (in terms of resource costs).
The first step is to make a helper method (or methods) that will accept the original date and time ( UTC or server time) and return the correct date and time.
In views, you can display dates and times at any time.span s, for example:

<p>
  <span class="datetime">
    @Html.GetDateTime(DateTime.Now)
  </span>
</p>
<p>
  <span class="datetime" data-timestamp="@Html.GetTimestamp()">
    @Html.GetDateTime(DateTime.Now)
  </span>
</p>

<p>Метод <b>Html.GetDateTime</b> вполне может возвращать дату в тегах, тогда будет:</p>
<p>@Html.GetDateTime(DateTime.Now)</p>
<p>а на выходе может быть:<p>
<p>
  <span class="datetime" data-timestamp="123">
    15.11.2016 21:11:21
  </span>
</p>
<p>
  <b>timestamp</b> - позволит обойти проблему с форматированием, 
  при расчете времени на стороне клиента.
</p>

It will be easier to work with UTC , but it is quite possible to convert the time from the server time, the main thing is not to get lost in time, especially between winter and summer :-)
You will have to tinker with representations. Although if your field names containing date and time are more or less unified, then there should not be any special problems.
Search and replace can be done using regular expressions where you need to infer through a helper function, but this can be tricky.
In code ( C# ) there should not be any particular problems with the replacement.
Replacement in the database, if necessary, can be automated. The only thing is, if GETDATE() is used somewhere , there may be problems, you will also have to take into account.
Try for a start with representations (an output) to understand.
And it is worth carrying out (in the code and database) a large-scale replacement of the server time with UTC - time will tell.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question