0

I'm trying to display Sofia Local Time for every user that enters my website so that it does not matter what's the time of the user or the server of my website deployed.

This is JS but it displays the user's time.How to display current time in Sofia/Bulgaria .I use asp.net for server side language

 <script>
          $(document).ready(function () {            
        var time = new Date().toLocaleString('en-US', { hour:'numeric', minute:'numeric' ,timeZone: 'Europe/Sofia' });
        document.getElementById("Time").innerHTML = time;

        setInterval(change, 60000);
        function change() {
            var x = (new Date().toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', timeZone: 'Europe/Sofia' }));
            document.getElementById('Time').innerHTML = x;
        }
    });
    </script>
Georgi Antonov
  • 1,581
  • 21
  • 40

1 Answers1

2

You can convert to various time zones using toLocaleString . Or use moment.js. Check this post. Here is a fiidle http://jsfiddle.net/tva7o4jx/

new Date().toLocaleString('en-US', { timeZone: 'Europe/Sofia' }) 
Community
  • 1
  • 1
meteor
  • 2,518
  • 4
  • 38
  • 52
  • what code do i need inside Date() so that it displays only Hours and Minutes and can you suggest good way to refresh the time every 1 minute – Georgi Antonov Jun 20 '15 at 14:52
  • you can use `setInterval` to refresh every 1 minute. And use formatters to get hh:mm format! Check the fiddle in answer – meteor Jun 20 '15 at 18:13
  • i dont know but somehow the code you scripted from jsfiddle is not working , and i can't see the logic for this issue $(document).ready(function () { var time = new Date().toLocaleString('en-US', { hour:'numeric', minute:'numeric' ,timeZone: 'Europe/Sofia' }); document.getElementById("Time").innerHTML = time; }); this is working but i now need to refresh it every 1minute – Georgi Antonov Jun 21 '15 at 18:09
  • it does work. You have to wait for 60 sec to see the first o/p and from then on it gets updated every 60 sec – meteor Jun 21 '15 at 18:19