使用偏移量计算 JavaScript 日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14134396/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:47:52  来源:igfitidea点击:

Calculating JavaScript date time with offset

javascriptdatedatetimeextjs

提问by RyanP13

I am experiencing some unexpected results with a GMT time zone offset calcultaion.

我在 GMT 时区偏移计算方面遇到了一些意想不到的结果。

I am using the ExtJS date class to help calculate the time that a message arrives from the server at GMT 0 to the user's local machine which is currently GMT +8.

我正在使用 ExtJS 日期类来帮助计算消息在 GMT 0 从服务器到达当前 GMT +8 的用户本地计算机的时间。

I thought that if i calculated the offset, then added that to the timestamp in seconds then i could use the following calculation to give me a string to format as i please.

我想,如果我计算了偏移量,然后以秒为单位将其添加到时间戳中,那么我可以使用以下计算为我提供一个字符串以进行格式化。

var d = new Date((stamp + offset) * 1000);

stamp is the date/time in seconds as is the offset.

戳是以秒为单位的日期/时间和偏移量。

This returns the current date and time at my location but plus 8 hours. If i do not add the offset then the time is correct.

这将返回我所在位置的当前日期和时间,但加上 8 小时。如果我不添加偏移量,那么时间是正确的。

Does the method new Date() automatically give me the date/time at local time?

方法 new Date() 是否会自动给我当地时间的日期/时间?

回答by GottZ

just insert a unix timestamp containing the thousandth of a second. that unix timestamp should be UTC 0 and javascript will look up the local timezone of the clients computer and will calculate it.

只需插入一个包含千分之一秒的 unix 时间戳。unix 时间戳应该是 UTC 0,javascript 将查找客户端计算机的本地时区并计算它。

as you can see behind thislink, there are some UTC methods wich give you the time without local time offset. maybe this might be what you search for (in case you really want to set the timezone yourself).

正如您在链接后面所看到的,有一些 UTC 方法可以为您提供没有本地时间偏移的时间。也许这可能是您搜索的内容(以防您真的想自己设置时区)。

edit:

编辑:

new Date().getTimezoneOffset()will show you the difference between the local timezone and utc but you should consider using the utc methods.

new Date().getTimezoneOffset()将向您展示本地时区和 utc 之间的差异,但您应该考虑使用 utc 方法。

回答by Tribe

Just adding this in case it helps others looking latter. May be wrong but works for what I needed.

只是添加这个以防它帮助其他人寻找后者。可能是错误的,但适用于我所需要的。

I have the user's offset stored in seconds.

我以秒为单位存储了用户的偏移量。

offset assumed in seconds change the (offset * 1000)to make sure offset gets converted to milliseconds if your offset not in seconds.

(offset * 1000)如果偏移量不是以秒为单位,则假设以秒为单位的偏移量更改以确保偏移量转换为毫秒。

function offsetDate(offset){
            var d = new Date(new Date().getTime() + (offset * 1000));
            var hrs = d.getUTCHours();
            var mins = d.getUTCMinutes();
            var secs = d.getUTCSeconds();
            //simple output
            document.write(hrs + ":" + mins + ":" + secs);

回答by Cerbrus

From the looks of it, stampis your local time including timezone offset. JavaScript has no knowledge of the timezone of the server, it's (mainly) a client-side language.

从它的外观来看,stamp是您的本地时间,包括时区偏移量。JavaScript 不知道服务器的时区,它(主要)是一种客户端语言。