Get client computer timezone in asp.net mvc

How to get timezone id (ex: Central Standard Time) of a client machine in asp.net mvc?

+3
source share
3 answers

This needs to be done on the Javascript side and send this value to the hidden field back to the server. Take a look at Date.getTimezoneOffset () .

+6
source

This information is not sent to the server, so there is no easy way to do this. One option is to find the IP address in the geolocation database. http://www.ip2location.com/ is one.

ajax javascript Date.getTimezoneOffset().

$.get('/User/SetTimeZone' + Date.getTimezoneOffset());

.

+2

If you have problems indicating the time on the client side, you can do the following I wrote an extension for the DateTime class that does the conversion for me

public static MvcHtmlString ToClientTime(this DateTime dateTime){
   var builder = new TagBuilder("span");
   builder.MergeAttribute("data-utc-time",dateTime.ToString());
   builder.SetInnerText(string.Format("{0} (UTC)", dateTime.ToString()));
   return new MvcHtmlString(builder.ToString());
}

then I added a javascript file and let momentjs handle the client side conversion

$(document).ready(function() {
  $("[data-utc-time]").text(function () {
    var utcTime = $(this).attr("data-utc-time");
    return moment.utc(utcTime, 'DD.MM.YYYY HH:mm').local().format('DD.MM.YYYY HH:mm');
});
+1
source

Source: https://habr.com/ru/post/1720194/


All Articles