Javascript:获取当前日期(毫秒)(UTC)(不使用字符串) [英] Javascript: Getting current date in milliseconds (UTC) (NO use of strings)

查看:173
本文介绍了Javascript:获取当前日期(毫秒)(UTC)(不使用字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,你可能会认为这个问题已经被问到了,但我觉得没有。我已经阅读过的所有解决方案都有这个拼图技巧(如 getUTCMonth()+ getUTCMinutes + ... )。
但是,我只想比较两个UTC(!)日期之间的经过秒数,这不适用。



众所周知,你可以得到当前(非UTC)日期由:

  var d = new Date(); 
var t_millis = d.getTime();

但这不是我想要的。我想以UTC为单位,以毫秒为单位将当前的系统日期设置为UTC ,所以不要乱码。 AFAIK变量 t_millis 将包含当前时间戳的毫秒值,而不是UTC。
(由于d也在格林尼治标准时间)。除非 getTime()执行一种隐式时区转换,即添加给出毫秒的偏移量但是我从来没有阅读过任何地方)



所以真的没有其他方法可以将偏移量添加到时间值?
我拼命地丢失了一个像其他语言一样的 getUTCTimeMillis()的函数。

解决方案

无论你的时区如何,时间的毫秒值将是一样的。也就是说,地球上没有任何时间间隔大于零的毫秒数。 (他们可能会有一个整数小时甚至几分钟,但不是秒或毫秒)。



那就是说,你从 getTime() 是UTC相对时间戳。如果两个世界各地不同点的Web浏览器同时创建一个Date对象,则它们都将从 .getTime()(假设时钟是同步的,这当然不太可能)。



这里:1338585185539这是我从浏览器获得的时间戳。我在德克萨斯州的奥斯汀,现在是下午4点13分(所以时间戳从之前的稍微)。将其插入机器上的Date实例,看看它的内容。



编辑—为了后代,这个时间戳是从1 2012年6月。)


Well, you might think that this question has already been asked, but I think it has not. The solutions I've read about all had this "jigsaw puzzle" technique (like getUTCMonth() + getUTCMinutes + ...). But as I only want to compare the elapsed seconds between two UTC (!) dates, this does not apply.

As everybody knows, you can get the current (non-UTC) date by:

var d = new Date();
var t_millis = d.getTime();

But this is NOT what I want. I'd like to have the current system date in UTC and in milliseconds, so not mess about with strings at all. AFAIK the variable t_millis will contain the millisecond value of the current timestamp in GMT, not UTC. (Since d is in GMT as well. Unless getTime() does a sort of implicit time zone conversion, i. e. adding the offset BEFORE giving out the milliseconds, but I've never read about that anywhere)

So is there really no other way than adding the offset to the time value? I'm desperately missing a function like getUTCTimeMillis() known from other languages.

解决方案

The millisecond value of the time-of-day is going to be the same regardless of your time zone. That is, there are no time zones on planet Earth that differ from one another by a number of milliseconds greater than zero. (They may differ by an integer number of hours or even minutes, but not seconds or milliseconds.)

That said, the value you get back from getTime() is a UTC-relative timestamp. If two web browsers at widely different spots on the globe create a Date object at the same time, they'll both get the same value from .getTime() (assuming the clocks are synchronized, which is of course highly unlikely).

Here: 1338585185539 That's a timestamp I just got from my browser. I'm in Austin, TX, and now it's 4:13 in the afternoon (so that timestamp will be from slightly before that). Plug it into a Date instance on your machine and see what it says.

(edit — for posterity's sake, that timestamp is from 1 June 2012.)

这篇关于Javascript:获取当前日期(毫秒)(UTC)(不使用字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆