Good day. Tell me, please, an easy and elegant way to display the date and time of a specific time zone on the site page. The site is powered by bootstrap 3. I understand that I need a script that displays the time, but I need to take a specific time of a particular time zone, i.e. not with pc user.
2 answers
While waiting, he found the answer:
<script language="javascript" type="text/javascript"> var d = new Date(); var month=new Array("Jan","Feb","Mar","Apr","May","June", "July","Aug","Sep","Oct","Nov","Dec"); document.write(d.getDate()+ " " + month[d.getMonth()] + " " + d.getFullYear() + " г."); </script> |
If you mean client JS, you first need to know the offset (it is still taken from the client computer), and then add the difference to the target time zone:
var offset = new Date().getTimezoneOffset(); var d = new Date(); d.setMinutes(d.getMinutes() - offset + 120); // отнимаем разницу offset, чтобы получить GMT время, // а потом прибавляем разницу до нужного часового пояса // в минутах, в моём примере, разница в 120 минут или 2 часа console.log(d); And if the iron needs exact data, then we take it with the help of PHP (or whatever is on the server) from the open service and send it to JS.
|