Javascript中Date.now和Date.getTime之间有什么区别?时区如何影响它们? [英] What is the difference between Date.now and Date.getTime in Javascript and how timezone affects them?

查看:891
本文介绍了Javascript中Date.now和Date.getTime之间有什么区别?时区如何影响它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解当客户端和服务器的时区可能不同时,如何在客户端和服务器上使用Javascript创建日期会影响查询结果。

I am trying to understand how creation of dates using Javascript on client and server both can affect query results when timezone of client and server can differ.

在客户端,我使用 Date.now()来保存时间戳信息

On client side, I am using Date.now() to save timestamp information

var time = Date.now()
console.log(time);




now()方法返回自1970年1月1日起经过的毫秒数
00:00:00 UTC到目前为止作为数字。

The now() method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a Number.

我的理解是这个时间戳独立于任何时区偏移,换句话说,它指向零时区偏移是否正确?

My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset. Is it correct?.

我们也可以使用以下代码获取此时间戳

We can also get this timestamp using the following code

var now = new Date();
time = now.getTime();
console.log(time);

此代码仍提供相同的时间戳信息(零时区偏移)。 Date.now()只是获取时间戳的快捷方式吗?

This code still gives the same timestamp information (at zero timezone offset). Is Date.now() just a quick way to get timestamp?

现在,从 Date.now构建日期()并在浏览器的控制台中显示它。

Now, constructing date from Date.now() and displaying it in browser's console.

var time = Date.now()
console.log(time);

var date = new Date(time)
var time = date.getTime()
console.log(time);

console.log(date);

在我看来,只有在浏览器控制台中显示时,日期才会在浏览器的本地时区格式化否则此日期实例仍保留零时区偏移的时间戳。 这是正确的吗?

In my opinion, it is only when displaying in browser's console, date is formatted in the browser's local timezone otherwise this date instance still holds timestamp at zero timezone offset. Is it correct?.

我也看到了答案将Javascript日期转换为UTC

var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
time = now_utc.getTime();
console.log(time);

此代码仍提供与 Date.now相同的时间戳信息( ) console.log 语句仅相差几毫秒,我认为是由于代码执行时间而不是任何时区差异。执行时,以下是该时间实例的输出:

This code still gives the same timestamp information as given by Date.now() and console.log statements were differing by few milliseconds only which I think is due to code execution time instead of any timezone difference. When executed, following was the output for that instance of time:

1468408292575
1468388492000

UTC时间戳与 Date.now()给出的时间戳之间是否存在差异

Is there any difference between a UTC timestamp and timestamp given by Date.now()?

您能否确认这三个选项是否产生相同的结果(即独立于任何时区)以及是否可以安全使用此时间戳信息表示客户端和服务器在不同时区的交互时间。示例:

Can you please confirm if these three options produce same result (i.e. independent of any timezone) and if it is safe to use this timestamp information for client and server interactions when they are in a different timezone. Examples:


  • 客户端向服务器端发送保存请求以保存此客户端生成的时间戳?

  • 客户端根据此保存的时间戳询问结果?

我想将此值降低到我只想显示时区信息的水平用于显示目的,例如在HTML中,在生成的文件中。

I want to reduce this to a level where I want to show timezone information only for display purpose e.g. in HTML, in generated files.

我的问题可能过于简单,因为我很困惑看到日期类文档正确使用它以避免时差问题。

My questions might be too simple because I am confused on seeing the Date class documentation on correctly using it to avoid time difference issues.

推荐答案

两者通常相同

var dt1 = Date.now(); is little faster

更多参考

链接1

链接2

这篇关于Javascript中Date.now和Date.getTime之间有什么区别?时区如何影响它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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