title: How to elegantly deal with timezones
body: <p>I have a website that is hosted in a different timezone than the users using the application. In addition to this, users can have a specific timezone. I was wondering how other SO users and applications approach this? The most obvious part is that inside the DB, date/times are stored in UTC. When on the server, all date/times should be dealt with in UTC. However, I see three problems that I'm trying to overcome:</p>
  <ol>
  <li><p>Getting the current time in UTC (solved easily with <code>DateTime.UtcNow</code>).</p>
  </li>
  <li><p>Pulling date/times from the database and displaying these to the user. There are potentially <em>lots</em> of calls to print dates on different views. I was thinking of some layer in between the view and the controllers that could solve this issue. Or having a custom extension method on <code>DateTime</code> (see below). The major down side is that at <em>every</em> location of using a datetime in a view, the extension method must be called!</p>
  <p>This would also add difficulty to using something like the <code>JsonResult</code>. You could no longer easily call <code>Json(myEnumerable)</code>, it would have to be <code>Json(myEnumerable.Select(transformAllDates))</code>. Maybe AutoMapper could help in this situation?</p>
  </li>
  <li><p>Getting input from the user (Local to UTC). For example, POSTing a form with a date would require converting the date to UTC before. The first thing that comes to mind is creating a custom <code>ModelBinder</code>.</p>
  </li>
  </ol>
  <p>Here's the extensions that I thought of using in the views:</p>
  <pre><code>public static class DateTimeExtensions
  {
      public static DateTime UtcToLocal(this DateTime source, 
          TimeZoneInfo localTimeZone)
      {
          return TimeZoneInfo.ConvertTimeFromUtc(source, localTimeZone);
      }
  
      public static DateTime LocalToUtc(this DateTime source, 
          TimeZoneInfo localTimeZone)
      {
          source = DateTime.SpecifyKind(source, DateTimeKind.Unspecified);
          return TimeZoneInfo.ConvertTimeToUtc(source, localTimeZone);
      }
  }
  </code></pre>
  <p>I would think that dealing with timezones would be such a common thing considering a lot of applications are now cloud-based where the server's local time could be much different than the expected time zone.</p>
  <p>Has this been elegantly solved before? Is there anything that I'm missing? Ideas and thoughts are much appreciated.</p>
  <p><strong>EDIT:</strong> To clear some confusion I thought add some more details. The issue right now isn't <em>how</em> to store UTC times in the db, it's more about the process of going from UTC-&gt;Local and Local-&gt;UTC. As @Max Zerbini points out, it's obviously smart to put the UTC-&gt;Local code in the view, but is using the <code>DateTimeExtensions</code> really the answer? When getting input from the user, does it make sense to accept dates as the user's local time (since that's what JS would be using) and then use a <code>ModelBinder</code> to transform to UTC? The user's timezone is stored in the DB and is easily retrieved.</p>