.Net Timezone
List of .Net Timezone Values- https://lonewolfonline.net/timezone-information/
How do you convert the date and time to a client’s or user’s time zone in Blazor Server? -
https://www.syncfusion.com/faq/blazor/javascript-interop/how-do-you-convert-the-date-and-time-to-a-clients-or-users-time-zone-in-blazor-server
This article will help to understand how to get client machine’s datetime with local timezone offset value.
Use javascript method getTimezoneOffset() and assign it’s value to hidden field in .cshtml file as shown in below code. Then pass this value to mvc controller.
var offset = new Date().getTimezoneOffset();
$('#timeZoneoffset').val(offset);
In the above code, variable offset will have the value which is the difference, in minutes, from local time to UTC. Note that this means if offset is positive the local timezone is behind UTC and negative if it is ahead.
Hence if client machine is in India then offset value will be (-330) and local timezone offset value will be (+05.30).
To get client machine’s datetime value with local timezone offset like (2019-04-04 11:05:41.31 +05:30) in controller refer the following code :
int timeZoneoffset = timeZoneoffset * -1;
TimeSpan spWorkMin = TimeSpan.FromMinutes(timeZoneoffset);
string offset2 = string.Format("{0:00}:{1:00}", (int)spWorkMin.TotalHours, spWorkMin.Minutes);
TimeSpan timeZoneOffset1 = ParseTimezoneOffset(offset2);
DateTimeOffset clientDatetime = DateTimeOffset.Now.ToOffset(timeZoneOffset1);
In the above code timeZoneoffset is offset value from javascript function.
- Get link
- X
- Other Apps
Comments
Post a Comment