How to get server timezone in javascript

I want to set a different timezone in Javascript. It currently shows the date and time zone of the local computer or client PC. Date Time.

Hi,

+1
source share
3 answers

Javascript is a client language and does not interact with the server in this way. You will need to extract this data from your server platform.

Here are some PHP code to get the data you are looking for. You will need to put this on your page and repeat the result in the JS variable ....

<?php $date = new DateTime(null, new DateTimeZone('Europe/London')); $tz = $date->getTimezone(); $tzone = $tz->getName(); ?> <script type="text/javascript"> var timeZone='<?php echo $tzone ?>'; </script> 

.... or leave the PHP page separate and retrieve data using AJAX


getTimeZone.php

 <?php $date = new DateTime(null, new DateTimeZone('Europe/London')); $tz = $date->getTimezone(); echo $tz->getName(); ?> 

Js

 var timeZone=null; $.get('getTimeZone.php', function(result){ timeZone=result; }, 'html'); //I know this is jQuery, not JS, but you get the idea. 
+6
source

JavaScript has no built-in functions.

You can embed a time zone in (for example) a hidden field on a page when rendering it from a server or implement some kind of HTTP request to actively retrieve it from the server.

+1
source

The only way to do this is to include the time zone in the server’s response or make an ajax call from the javascript browser client on the server to get the server time zone.

0
source

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


All Articles