Javascript 使用绝对时区的Javascript倒计时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8805613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript countdown using absolute timezone?
提问by levi
I have a javascript countdown timer that works by taking a specified date and time, and comparing it to the current date and time. The issue is, the current time is relative to the users timezone, so the time remaining is different between users.
我有一个 javascript 倒数计时器,它通过获取指定的日期和时间并将其与当前日期和时间进行比较来工作。问题是,当前时间是相对于用户时区的,所以用户之间的剩余时间是不同的。
How can I have the timer countdown till a time in a specific timezone, in my case GMT -5 hours?
我怎样才能让计时器倒计时到特定时区的时间,在我的情况下 GMT -5 小时?
I understand i can use the below code to get the users timezone, but I am lost as how to use this.
我知道我可以使用下面的代码来获取用户时区,但我不知道如何使用它。
myDateObj.getTimezoneOffset( ) / 60
myDateObj.getTimezoneOffset() / 60
采纳答案by Brandon Boone
A quick search reveals: convert-the-local-time-to-another-time-zone-with-this-javascript
快速搜索显示:convert-the-local-time-to-another-time-zone-with-this-javascript
Following the article verbatim gets you this example:
按照文章逐字逐句为您提供这个示例:
var d = new Date();
var localTime = d.getTime();
var localOffset = d.getTimezoneOffset() * 60000;
var utc = localTime + localOffset;
// obtain and add destination's UTC time offset
// for example, Bombay
// which is UTC + 5.5 hours
var offset = 5.5;
var bombay = utc + (3600000*offset);
var nd = new Date(bombay);
alert("Bombay time is " + nd.toLocaleString() + "<br>");
jsFiddle: http://jsfiddle.net/GEpaH/
jsFiddle:http: //jsfiddle.net/GEpaH/
Just update with your desired offset and you should be all set.
只需更新您所需的偏移量,您就应该准备就绪。
回答by kennebec
You can use Date.UTC(year,month,day,hours,minutes,seconds,msec)
您可以使用 Date.UTC(year,month,day,hours,minutes,seconds,msec)
It operates just like the Date constructor, but returns the timestamp of the arguments at Greenwich time (offset=0) instead of local time.
它的操作就像 Date 构造函数,但返回格林威治时间 (offset=0) 而不是本地时间的参数时间戳。
var localtime=new Date(Date.UTC(year,month,day,hours,minutes,seconds,msec))
returns the local time for the UTC time specified.
返回指定 UTC 时间的本地时间。
Everyone (whose clock is set correctly) will end the countdown together.
每个人(时钟设置正确)将一起结束倒计时。
回答by gregers
Just create a Date with an RFC 2822-timestampwith timezone. That time will then be converted to the users current location (based on OS settings). Even with corrections for daylight savings time!
只需使用带时区的RFC 2822 时间戳创建日期。然后该时间将转换为用户当前位置(基于操作系统设置)。即使对夏令时进行更正!
I'm in Norway, which currently is in daylight savings time, so it's GMT+2. Here is what happens when I create a Date object using GMT-0500:
我在挪威,目前处于夏令时,所以现在是 GMT+2。以下是我使用 GMT-0500 创建 Date 对象时发生的情况:
var myDateObj = new Date("Fri Apr 17 2015 12:00:00 GMT-0500 (CDT)");
myDateObj.toString();
Fri Apr 17 2015 19:00:00 GMT+0200 (CEST)
2015 年 4 月 17 日星期五 19:00:00 GMT+0200 (CEST)
How to get the correct date string for your location? If it's the timezone you're currently in; just do myDateObj.toString()
in your browsers dev-tools console. For a different timezone; change the timezone in your operating system first. (Or read the RFC)
如何获取您所在位置的正确日期字符串?如果是您当前所在的时区;只需myDateObj.toString()
在您的浏览器开发工具控制台中进行即可。对于不同的时区;首先更改操作系统中的时区。(或阅读 RFC)
new Date().toString();
Fri Apr 17 2015 12:36:57 GMT+0200 (CEST)
2015 年 4 月 17 日星期五 12:36:57 GMT+0200 (CEST)
回答by Cam
To revise the approach Brandon has taken to calculate a UTC-shifted time, we can slim the code down into a two-line extension of the Date object:
为了修改 Brandon 用于计算 UTC 偏移时间的方法,我们可以将代码精简为 Date 对象的两行扩展:
/* getOffsetDate - Returns a Date shifted by a certain offset
* @param offset the UTC offset to shift, in hours
* @return new date object shifted by UTC offset
*/
Date.prototype.getOffsetDate = function( offset ) {
utc = this.getTime() + (this.getTimezoneOffset() * 60000);
return new Date(utc + (3600000*offset));
}
You can then calculate the UTC-5 shifted date as follows:
然后,您可以按如下方式计算 UTC-5 偏移日期:
myDate = new Date().getOffsetDate(-5);
It should be noted that extending native prototypes in this manner is generally considered a bad practice since it muddles core objects that other libraries depend upon. To justify it, you'd have to argue this functionality should be a native part of the Date object.
应该注意的是,以这种方式扩展原生原型通常被认为是一种不好的做法,因为它混淆了其他库所依赖的核心对象。为了证明这一点,您必须争辩说此功能应该是 Date 对象的本机部分。
回答by SirCommy
You can't get an accurate date with JavaScript as it is client-side and it is based on the user's operating system clock. You can use jCounterto display countdowns based on server-side timezones.
您无法使用 JavaScript 获得准确的日期,因为它是客户端并且基于用户的操作系统时钟。您可以使用jCounter根据服务器端时区显示倒计时。
But hey, if you really want to do it yourself, download jCounter and you'll find a dateandtime.php file as well which retrieves the current date server-side, as timestamp (it will have to be placed on a server btw, not on your desktop :P)
但是,嘿,如果你真的想自己做,下载 jCounter,你会发现一个 dateandtime.php 文件,它检索服务器端的当前日期,作为时间戳(顺便说一句,它必须放在服务器上,而不是在你的桌面上 :P)
Check how that script uses that file and retrieves the real current date to operate against it and calculate accurate countdowns.
检查该脚本如何使用该文件并检索实际当前日期以对其进行操作并计算准确的倒计时。
Cheers
干杯
回答by Murray McDonald
You don't really say exactly what your are trying to accomplish. The javascript date object retrieves the local time and the "timezone offset" (relative to GMT (UTC)). These are of course "set" by the user so even if in the same time zone, two users are very unlikely to have the same "time".
你并没有真正说出你想要实现的目标。javascript 日期对象检索本地时间和“时区偏移量”(相对于 GMT (UTC))。这些当然是由用户“设置”的,所以即使在同一时区,两个用户也不太可能有相同的“时间”。
If you are trying to time between different users you need to be referencing some centralized time authority.
如果您尝试在不同用户之间计时,则需要参考一些集中的时间权限。
I would use an AJAX type call (XMLHttpRequest) to a page own my own server that returns my server's time. That way each user is referencing the same time.
我将使用 AJAX 类型调用 (XMLHttpRequest) 来访问拥有我自己的服务器的页面,该服务器返回我的服务器时间。这样每个用户都在引用相同的时间。
Google XMLHttpRequest to find examples of the JS code you need (and oftentimes the corresponding server-side code for a simple service such as this).
使用 Google XMLHttpRequest 查找您需要的 JS 代码示例(通常是此类简单服务的相应服务器端代码)。
PS: I would also install some simple client software to keep the time on my server accurate by synching with an atomic clock every 10 minutes.
PS:我还会安装一些简单的客户端软件,通过每 10 分钟与原子钟同步一次来保持服务器上的时间准确。