title: DateTime.Now and Culture/Timezone specific
body: <p>Our application was designed to handle user from different Geographic location. </p>
  
  <blockquote>
    <p>We are unable to detect what is the current end user local time and
    time zone operate on it. They select different culture like sv-se,
    en-us, ta-In even they access from Europe/London timezone..</p>
  </blockquote>
  
  <p>We hosted it in a hosting server in US, application users are from <code>Norway/Denmark/Sweden/UK/USA/India</code></p>
  
  <p>The problem is we used <code>DateTime.Now</code> to store the record created/updated date, etc.</p>
  
  <p>Since the <strong><em>Server runs in USA</em></strong> all user data are saved as US time :(</p>
  
  <p>After researching in SO, we decided to stored all history dates in DB as <code>DateTime.UtcNow</code></p>
  
  <p><strong>PROBLEM:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/NVQD7.png" alt="enter image description here"></p>
  
  <p>There is a record created on <code>29 Dec 2013, 3:15 P.M Swedish time</code>. </p>
  
  <pre><code> public ActionResult Save(BookingViewModel model)
      {
          Booking booking = new Booking();
          booking.BookingDateTime = model.BookingDateTime; //10 Jan 2014 2:00 P.M
          booking.Name = model.Name;
          booking.CurrentUserId = (User)Session["currentUser"].UserId;
          //USA Server runs in Pacific Time Zone, UTC-08:00
          booking.CreatedDateTime = DateTime.UtcNow; //29 Dec 2013, 6:15 A.M
          BookingRepository.Save(booking);
          return View("Index");
      }
  </code></pre>
  
  <p>We want to show the same history time to the user who logged in in India/Sweden/USA.</p>
  
  <p>As of now we are using current culture user logged in and choose the timezone from a config file and using for conversion with TimeZoneInfo class</p>
  
  <pre><code>&lt;appSettings&gt;
      &lt;add key="sv-se" value="W. Europe Standard Time" /&gt;
      &lt;add key="ta-IN" value="India Standard Time" /&gt;
  &lt;/appSettings&gt;
  
      private DateTime ConvertUTCBasedOnCulture(DateTime utcTime)
      {
          //utcTime is 29 Dec 2013, 6:15 A.M
          string TimezoneId =                  
                  System.Configuration.ConfigurationManager.AppSettings
                  [System.Threading.Thread.CurrentThread.CurrentCulture.Name];
          // if the user changes culture from sv-se to ta-IN, different date is shown
          TimeZoneInfo tZone = TimeZoneInfo.FindSystemTimeZoneById(TimezoneId);
  
          return TimeZoneInfo.ConvertTimeFromUtc(utcTime, tZone);
      }
      public ActionResult ViewHistory()
      {
          List&lt;Booking&gt; bookings = new List&lt;Booking&gt;();
          bookings=BookingRepository.GetBookingHistory();
          List&lt;BookingViewModel&gt; viewModel = new List&lt;BookingViewModel&gt;();
          foreach (Booking b in bookings)
          {
              BookingViewModel model = new BookingViewModel();
              model.CreatedTime = ConvertUTCBasedOnCulture(b.CreatedDateTime);
              viewModel.Add(model);
          }
          return View(viewModel);
      }
  </code></pre>
  
  <p>View Code</p>
  
  <pre><code>   @Model.CreatedTime.ToString("dd-MMM-yyyy - HH':'mm")
  </code></pre>
  
  <p>NOTE: The user can change the culture/language before they login. Its a localization based application, running in US server. </p>
  
  <p>I have seen <a href="http://nodatime.org" rel="nofollow noreferrer"><code>NODATIME</code></a>, but I could not understand how it can help with multi culture web application hosted in different location.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I show a same record creation date <code>29 Dec 2013, 3:15 P.M</code> for the users logged in INDIA/USA/Anywhere`? </p>
  
  <p>As of now my logic in <code>ConvertUTCBasedOnCulture</code> is based user logged in culture. This should be irrespective of culture, since user can login using any culture from India/USA</p>
  
  <p><strong>DATABASE COLUMN</strong></p>
  
  <p>CreatedTime: <code>SMALLDATETIME</code></p>
  
  <p><strong><em>UPDATE: ATTEMPTED SOLUTION:</em></strong></p>
  
  <p><code>DATABASE COLUMN TYPE: DATETIMEOFFSET</code></p>
  
  <p><strong>UI</strong></p>
  
  <p>Finally I am sending the current user's local time using the below Momento.js code in each request</p>
  
  <pre><code>$.ajaxSetup({
      beforeSend: function (jqXHR, settings) {
          try {
        //moment.format gives current user date like 2014-01-04T18:27:59+01:00
              jqXHR.setRequestHeader('BrowserLocalTime', moment().format());
          }
          catch (e) {
          }
      }
  });
  </code></pre>
  
  <p><strong>APPLICATION</strong></p>
  
  <pre><code>public static DateTimeOffset GetCurrentUserLocalTime()
  {
      try
      {
        return 
        DateTimeOffset.Parse(HttpContext.Current.Request.Headers["BrowserLocalTime"]);
      }
      catch
      {
          return DateTimeOffset.Now;
      }
  }
  </code></pre>
  
  <p>then called in</p>
  
  <pre><code> model.AddedDateTime = WebAppHelper.GetCurrentUserLocalTime();
  </code></pre>
  
  <p>In View</p>
  
  <pre><code>@Model.AddedDateTime.Value.LocalDateTime.ToString("dd-MMM-yyyy - HH':'mm")
  </code></pre>
  
  <p>In view it shows the local time to user, however I want to see like <code>dd-MMM-yyyy CET/PST</code> (2 hours ago).</p>
  
  <p>This 2 hours ago should calculate from end user's local time. Exactly same as stack overflow question created/edited time with Timezone display and local user calculation.</p>
  
  <p><strong>Example:</strong> <code>answered Jan 25 '13 at 17:49 CST (6 hours/days/month ago)</code> So the other viewing from USA/INDIA user can really understand this record was created exactly 6 hours from INDIA/USA current time</p>
  
  <p>Almost I think I achieved everything, except the display format &amp; calculation. How can i do this? </p>