Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This can be done on the server side if you have a <code>TimeZoneInfo</code> object. You can use the static <code>ConvertTimeFromUtc()</code> method.</p> <p>In C#:</p> <pre><code>DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(myDbDateTime, myTimeZoneInfo); </code></pre> <p>If you do not have the timezone on the server side things get tricky since javascript does not provide the client's timezone (unless they are in the US, and even then only on some browsers). In this case, it may be best to force the user to select their current timezone and store it against their account. If this is being displayed to anonymous users, you should probably display in UTC by default and provide an option to refresh it for a selected timezone.</p> <p><strong>Update</strong></p> <p>There are several issues which appear when trying to automatically determine a user's timezone.</p> <ol> <li>Timezone is not provided to the server by the user agent.</li> <li>Javascript does not provide access to the timezone (except in some browsers, sometimes).</li> </ol> <p>The javascript function <code>getTimezoneOffset()</code> may initially sound like a good idea, but since there are multiple timezones with the same offset, this is not a unique value. The difference between many of these non-unique zones is their implementation of daylight saving time. </p> <blockquote> <p>Example: Indiana does not regard DST. Therefore, for half the year their offset matches eastern time, while the other half their offset is equal to central time.</p> </blockquote> <p>If, however, your user base is located primarily in the US and uses IE, Chrome, Safari, or Firefox, than you can use the <code>toString()</code> method on a <code>Date</code> object to obtain the timezone. These browsers append the timezone to the date string in different ways. Outside the US, the timezone is not included in all browsers (though some may still show it). </p> <blockquote> <p><strong>Open <a href="http://jsbin.com/onulo3" rel="noreferrer">http://jsbin.com/onulo3</a> to observe:</strong><br> <strong>IE8:</strong> Sun Feb 14 22:12:22 EST 2010<br> <strong>Chrome:</strong> Sun Feb 14 2010 22:12:22 GMT-0500 (Eastern Standard Time)<br> <strong>Safari:</strong> Sun Feb 14 2010 22:12:22 GMT-0500 (Eastern Standard Time)<br> <strong>Firefox:</strong> Sun Feb 14 2010 22:12:22 GMT-0500 (Eastern Standard Time)</p> </blockquote> <p>With some parsing, you can now determine the timezone for all your American users. For everyone else you can display the time in UTC (with a notice to that effect).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload