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

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

问题描述

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

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.

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

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

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

但这不是我想要的。我想以毫秒为单位以UTC 表示当前的系统日期,所以根本不要乱用字符串。 AFAIK变量 t_millis 将包含GMT当前时间戳的毫秒值,而不是UTC。
(因为 d 也在GMT中。除非 getTime()执行一种隐式时区转换,即添加偏移量 BEFORE 给出毫秒,但我从来没有在任何地方读过这些内容)

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)

所以除了添加偏移之外别无他法时间价值?
我很想念其他语言中的 getUTCTimeMillis()这样的函数。

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.

推荐答案

这是一个老问题,但为了新访问者,这里是正确答案:

This is an old question but for the sake of the new visitors here is the CORRECT answer:

Date.now();

它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数

It returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC

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

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