ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Convert DateTime to Local with TimeZone using C# and VB.Net in ASP.Net<p>I will get back to you soon.</p>, 21 Aug 2019 01:21:39 GMTConvert DateTime to Local with TimeZone using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="Thilan"> Thilan</a>,</p> <p>First get the server timezone.</p> <p>Then get the client timezone using JavaScript.</p> <p>Then using ConvertTime method of TimeZoneInfo, convert time to local time.</p> <p>for more details refer below link.</p> <p><a href=""></a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34;&gt; window.onload = function () { var date = new Date(); var tzstr = date.toTimeString().split(&#34;(&#34;); var timezone = tzstr[1].toString().replace(&#34;)&#34;, &#34;&#34;); document.getElementById(&#39;hfTimezone&#39;).value = timezone; }; &lt;/script&gt; &lt;asp:HiddenField ID=&#34;hfTimezone&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button Text=&#34;Get&#34; runat=&#34;server&#34; OnClick=&#34;GetLocal&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">protected void GetLocal(object sender, EventArgs e) { // 21-Aug-2019 11:59:59 AM DateTime time = ConvertDateTimeToLocal(new DateTime(2019, 08, 21, 11, 59, 59)); } protected DateTime ConvertDateTimeToLocal(DateTime dateTime) { TimeZoneInfo serverTimeZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id); // Get Server TimeZone TimeZoneInfo clientTimeZone = TimeZoneInfo.FindSystemTimeZoneById(hfTimezone.Value); // Get client TimeZone DateTime localTime = TimeZoneInfo.ConvertTime(dateTime, serverTimeZone, clientTimeZone); // Convert to local time return localTime; }</pre> <p><strong><span style="text-decoration: underline;">VB.Net</span></strong></p> <pre class="brush: vb">Protected Sub GetLocal(ByVal sender As Object, ByVal e As EventArgs) &#39; 21-Aug-2019 11:59:59 AM Dim time As DateTime = ConvertDateTimeToLocal(New DateTime(2019, 8, 21, 11, 59, 59)) End Sub Protected Function ConvertDateTimeToLocal(ByVal dateTime As DateTime) As DateTime Dim serverTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id) &#39;Get Server TimeZone Dim clientTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(hfTimezone.Value) &#39; Get client TimeZone Dim localTime As DateTime = TimeZoneInfo.ConvertTime(dateTime, serverTimeZone, clientTimeZone) &#39; Convert to local time Return localTime End Function</pre> <p>Here i am saving the client TimeZone in HiddenField on load.</p>, 21 Aug 2019 06:03:36 GMT