admin管理员组文章数量:1328027
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
3 Answers
Reset to default 5You 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
本文标签: How to get the Australian Time Zone using Javascript (Not JQuery)Stack Overflow
版权声明:本文标题:How to get the Australian Time Zone using Javascript? (Not JQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237878a2438410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论