WorkingScript
WorkingScript

Reputation: 27

How to get the Australian Time Zone using Javascript? (Not JQuery)

I am trying to help a friend to get the Australian Time Zone for the University Assignment and finding difficulty. Could someone point us in the right direction? Thank you!

<script>
function Timezone() {
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
document.getElementById("add").innerHTML = currentTimeZoneOffsetInHours;
}
</script>

<p id="add"></p>

Upvotes: 0

Views: 10720

Answers (3)

nealous3
nealous3

Reputation: 742

By looking at your code, looks like you are trying to get the current date and time of an Australian timezone. Lets say you want Australian Eastern Standard Time (AEST) and you want the date displayed how they would in Australia DD-MM-YYYY then do the following:

var timestamp_UTC = new Date();
var readable_timestamp_AEST = timestamp_UTC.toLocaleDateString("en-AU", {timeZone: "Australia/Sydney"}).replace(/\//g, "-") + ' ' + somestamp.toLocaleTimeString("en-AU", {timeZone: "Australia/Sydney"});

"en-AU" is the locales argument which tells the toLocalDateString to display the date as DD-MM-YYYY and the second argument is for options (timeZone is just one such possible option). Info about toLocalDateString function can be found here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Here is some information about the Date() function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Hope this clears up a few things around getting times and dates from the Date() function.

Upvotes: 2

Aran Tamool
Aran Tamool

Reputation: 309

You simply use

let AuDate = new Date().toLocaleString("en-US", {timeZone: "Australia/Sydney"});

Upvotes: 5

Sandman
Sandman

Reputation: 2755

I think i understand what you mean. But before that i'd like to make 2 points:

1: The Timezone() function should be called somewhere.

<script>
  function Timezone() {
    var x = new Date();
    var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
    document.getElementById("add").innerHTML = currentTimeZoneOffsetInHours;
  }
  Timezone();
</script>

2: The convention usually is that methods start with a lower case letter. Maybe updateTimezone() would be more appropriate.

Your question can be interpreted in 2 ways now:

  • you want your timezone's offset in hours and for this the code above should work. getTimezoneOffset() is the way to go.
  • you want a human readable name of your timezone, as you can see on my site currentmillis.com (in my case it says GTB Summer). You can look in my source code to see how i achieve this:
var s = date.toString();
var iOfP = s.indexOf('('); // index of parenthesis
if (iOfP < 0) {
  s = s.substring(s.lastIndexOf(' ') + 1);
} else {
  s = s.substring(iOfP+1, s.length-1);
}
if (s.length > 4 && s.lastIndexOf(" Time") == s.length-5){
  s = s.substring(0, s.length-5);
}
timezoneM.innerHTML = s;

This works because when you call toString() on the date the result should contain the full name of your timezone: w3schools.com/jsref/jsref_tostring_date.asp

Upvotes: 0

Related Questions