Javascript 以毫秒为单位(UTC)获取当前日期(不使用字符串)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10857272/
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
Getting current date in milliseconds (UTC) (NO use of strings)
提问by syntaxerror
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.
好吧,你可能认为这个问题已经被问到了,但我认为它没有。我读过的所有解决方案都有这种“拼图”技术(如getUTCMonth() + getUTCMinutes + ...
)。但是因为我只想比较两个 UTC (!) 日期之间经过的秒数,所以这并不适用。
As everybody knows, you can get the current (non-UTC) date by:
众所周知,您可以通过以下方式获取当前(非 UTC)日期:
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 andin 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. UnlessgetTime() does a sort of implicit time zone conversion, i. e. adding the offset BEFOREgiving out the milliseconds, but I've never read about that anywhere)
但这不是我想要的。我想要以 UTC和毫秒为单位的当前系统日期,所以根本不要乱用字符串。AFAIK 该变量t_millis
将包含 GMT 中当前时间戳的毫秒值,而不是 UTC。(因为d
也是格林威治标准时间。除非getTime() 进行某种隐式时区转换,即在给出毫秒之前添加偏移量,但我从未在任何地方读到过)
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.
那么除了将偏移量添加到时间值之外,真的没有其他方法吗?我非常想念getUTCTimeMillis()
其他语言中已知的功能。
回答by advncd
This is an old question but for the sake of the new visitors here is the CORRECTanswer:
这是一个老问题,但为了新访客,这里是正确的答案:
Date.now();
It returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC
它返回自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的毫秒数
回答by Pointy
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()
isa 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).
也就是说,您返回的值getTime()
是相对于 UTC 的时间戳。如果位于地球上不同地点的两个 Web 浏览器同时创建一个 Date 对象,它们将获得相同的值.getTime()
(假设时钟是同步的,这当然不太可能)。
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.
此处:1338585185539 这是我刚从浏览器中获取的时间戳。我在德克萨斯州奥斯汀,现在是下午 4 点 13 分(因此时间戳将在此之前稍早一点)。将它插入机器上的 Date 实例,看看它说了什么。
(edit— for posterity's sake, that timestamp is from 1 June 2012.)
(编辑- 为了后代的缘故,该时间戳是从 2012 年 6 月 1 日开始的。)
回答by kirpit
how about:
怎么样:
var now = new Date();
var utc_now = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
console.log('UTC: ' + utc_now) // correct UTC time but wrong timezone!
console.log('UTC (in ms): ' + utc_now.getTime())
回答by Blacknet
I have used this function to solve the problem.
我已经使用这个功能来解决这个问题。
function getUTCNow()
{
var now = new Date();
var time = now.getTime();
var offset = now.getTimezoneOffset();
offset = offset * 60000;
return time - offset;
}
- The getTime function returns the number of milliseconds elapsed since 1 January 1970 00:00:00 in the client timezone.
- getTimezoneOffset return offset in minutes between Client timezone and UTC.
- offset = offset * 60000; this operation transform minutes in miliseconds.
- subtracting the offset get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
- getTime 函数返回客户端时区自 1970 年 1 月 1 日 00:00:00 以来经过的毫秒数。
- getTimezoneOffset 返回客户端时区和 UTC 之间以分钟为单位的偏移量。
- 偏移量 = 偏移量 * 60000;此操作以毫秒为单位转换分钟。
- 减去偏移量得到自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的毫秒数。
回答by Darlesson
To get the timestamp from a date in UTC, you need to take in consideration the timezone and daylight savings for that date. For example, a date in January or in July could mean 1 hour difference.
要从 UTC 日期中获取时间戳,您需要考虑该日期的时区和夏令时。例如,一月或七月的日期可能意味着 1 小时的差异。
The option below does just that.
下面的选项就是这样做的。
Date.prototype.getUTCTime = function () {
return this.getTime() - (this.getTimezoneOffset() * 60000);
};
It can be used as in:
它可以用于:
var date = new Date();
var timestamp = date.getUTCTime();