0

I'm trying to display current time of Dallas and Australia and I have come up with the Code,but it's not working . I'm completely new to Java script and Java here is my code , please help me out in achieving this

<script>
var now = new Date(); 
var now_utc = now.getUTCHours()-6+":"+ now.getUTCMinutes()+":"+ now.getUTCSeconds() 
document.getElementById("inner1").innerHTML+=now_utc;

var aus= new Date();
var aus_time= aus.getUTCHours()+11
document.getElementById("inner2").innerHTML+=aus_time;

  </script>

I want to display Current time of Dallas,London,Australia.

Thanks you all.

3 Answers3

1

As once was pointed out by an Apple Engineer: never ever do calendar calculations by yourself. There are tons of small and large pitfalls that most people are unaware of and thus are not taking into account.

If you need an accurate depiction of the (current) time in another Timezone there is no other way then use a library that has all the information regarding Timezones and its peculiarities. If you do not care about being exact every day of the year then this might be overkill but you have to decide for yourself.

For Javascript there exists the https://github.com/mde/timezone-js library, which itself uses the Olsen Database (https://en.wikipedia.org/wiki/Tz_database).

You have to download the Timezone-Infos and provide them along your source code. Then you can do all that TZ-related calculations/

// 'now' in the Timezone of Chicago (Central Time, should be equal to that of Dallas)
var dt_chicago = new timezoneJS.Date('America/Chicago');
// 'now' in London 
var dt_london = new timezoneJS.Date('Europe/London');
// 'now' in Brisbane - you should look up which timezones 
// there are in Australia and which you want to display
var dt_brisbane = new timezoneJS.Date('Australia/Brisbane');

I just found out that there is another library http://momentjs.com/timezone/ but I have not used it yet so I may not recommend or advise against using it.

Martin Schneider
  • 3,268
  • 4
  • 19
  • 29
0

The below piece of code will calculate the current time of any specific time zone. Just pass the time zone of any specific country it will show the current time.

function calcTime(city, offset) {
   var d = new Date();
   var utc = d.getTime() - (d.getTimezoneOffset() * 60000);
   var nd = new Date(utc + (3600000*offset));
   return "The local time for city"+ city +" is "+ nd.toLocaleString();
}
console.log(calcTime('Dhaka', '+6.0')));
console.log(calcTime('Australia', '+11.0')));
SM Farhad Ali
  • 1,063
  • 2
  • 12
  • 28
0

You use now variable break your script.On live var aus_time you forget semicolon at this line. As @Traktor53 said in comment AU have multiple timezone.

  • Your code is OS dependent you will check after change your system time.you will get wrong time. Its better way to use google time API.

    function gettz(){
    
    var dnow = new Date(); 
    var dnow_utc = dnow.getUTCHours()-6+":"+ dnow.getUTCMinutes()+":"+ dnow.getUTCSeconds() 
    document.getElementById("inner1").innerHTML+=dnow_utc;
    
    var aus= new Date();
    var aus_time= aus.getUTCHours()+11;
    document.getElementById("inner2").innerHTML+=(aus_time);
    return;
    }
    
    </script>
    
Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52