Hi Thilan,
First get the server timezone.
Then get the client timezone using JavaScript.
Then using ConvertTime method of TimeZoneInfo, convert time to local time.
for more details refer below link.
https://docs.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones
HTML
<script type="text/javascript">
window.onload = function () {
var date = new Date();
var tzstr = date.toTimeString().split("(");
var timezone = tzstr[1].toString().replace(")", "");
document.getElementById('hfTimezone').value = timezone;
};
</script>
<asp:HiddenField ID="hfTimezone" runat="server" />
<asp:Button Text="Get" runat="server" OnClick="GetLocal" />
C#
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;
}
VB.Net
Protected Sub GetLocal(ByVal sender As Object, ByVal e As EventArgs)
' 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) 'Get Server TimeZone
Dim clientTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(hfTimezone.Value) ' Get client TimeZone
Dim localTime As DateTime = TimeZoneInfo.ConvertTime(dateTime, serverTimeZone, clientTimeZone) ' Convert to local time
Return localTime
End Function
Here i am saving the client TimeZone in HiddenField on load.