最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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>

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>
Share Improve this question asked May 1, 2015 at 10:02 WorkingScriptWorkingScript 271 gold badge1 silver badge4 bronze badges 2
  • 1 What issues are you running into? If you could be more specific maybe people can help. Also, an example on jsfiddle is always nice :) – JosiahDaniels Commented May 1, 2015 at 14:05
  • What do you mean by "the Australian Time Zone"? Australia has multiple time zones, and different offsets apply at different times of the year for some of them. Read time in Australia, the timezone tag wiki and my blog post, What is a Time Zone. Also consider watching my Pluralsight course, Date and Time Fundamentals. – Matt Johnson-Pint Commented May 4, 2015 at 0:26
Add a ment  | 

3 Answers 3

Reset to default 5

You simply use

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

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/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Here is some information about the Date() function https://developer.mozilla/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.

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. (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./jsref/jsref_tostring_date.asp

发布评论

评论列表(0)

  1. 暂无评论