简体   繁体   English

Javascript日期对象基于什么

[英]Javascript date object based on what

I am using the date object below to obtain the current time from the user:我正在使用下面的日期对象从用户那里获取当前时间:

    var currentdate = new Date();
    return (currentdate.getDate() + "/"
            + (currentdate.getMonth()+1)  + "/"
            + currentdate.getFullYear() + " @ "
            + currentdate.getHours() + ":"
            + currentdate.getMinutes() + ":"
            + currentdate.getSeconds());

My question is where does currentdate obtain the time from?我的问题是currentdate从哪里获取时间? Does it grab the time from a central server?它是否从中央服务器获取时间? Would any user from anywhere around the globe execute the codes above at the same time will obtain the same value?来自全球任何地方的任何用户同时执行上述代码会获得相同的值吗?

Or this is per user based, meaning if two users from different time zones execute the codes at the same time, they will obtain the time based on their time zone, hence the codes return different values?或者这是基于每个用户的,这意味着如果来自不同时区的两个用户同时执行代码,他们将根据他们的时区获取时间,因此代码返回不同的值?

This is based on the Unix timestamp and will be based on the users' computers time if requested client-side, or the servers timestamp if requested server-side.这是基于 Unix 时间戳的,如果客户端请求,则基于用户的计算机时间,如果请求服务器端,则基于服务器时间戳。

For further reading see:如需进一步阅读,请参见:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

In the code you provided, the first line is:在您提供的代码中,第一行是:

var currentdate = new Date();

The constructor of the Date object is called without any parameters, and thus is initialized with a timestamp having the same value as Date.now() . Date对象的构造函数在没有任何参数的情况下被调用,因此使用与Date.now()具有相同值的时间戳进行初始化。 That value is a Unix timestamp , containing milliseconds since 1970-01-01 00:00:00.000 UTC (not accounting for leap seconds).该值是一个Unix 时间戳,包含自1970-01-01 00:00:00.000 UTC以来的毫秒数(不考虑闰秒)。 It is strictly UTC based.它严格基于UTC The sytem time zone is not involved in the construction, and the Date object does not store the system time zone.系统时区不参与构建, Date对象不存储系统时区。

Only the system clock is involved.只涉及系统时钟 Every computer system has an internal clock that can return the current UTC time.每个计算机系统都有一个内部时钟,可以返回当前的 UTC 时间。 This clock is most often synchronized with clocks of other computers via NTP, PPTP, or similar mechanisms.该时钟通常通过 NTP、PPTP 或类似机制与其他计算机的时钟同步。 It is computer dependent though.虽然它依赖于计算机。 For example, when configured for "set time automatically", Windows computers will obtain their time either from a public NTP server on the Internet, or from a domain controller on a local network, depending on the network configuration.例如,当配置为“自动设置时间”时,Windows 计算机将从 Internet 上的公共 NTP 服务器或本地网络上的域控制器获取时间,具体取决于网络配置。 However, a standalone computer that is not configured to set time automatically, could have a very different clock value.但是,未配置为自动设置时间的独立计算机可能具有非常不同的时钟值。

Thus, two given computers calling new Date() (or Date.now() ) will likely retrieve similar values if their clocks are synchronized correctly, but are not guaranteed to.因此,如果两个给定的计算机调用new Date() (或Date.now() ),如果它们的时钟正确同步,则可能会检索相似的值,但不能保证。 There is no network call involved, only a call to the local system clock.不涉及网络调用,只调用本地系统时钟。

In the rest of your code, you call the functions getMonth , getFullYear , getHours , getMinutes , and getSeconds .在其余代码中,您调用函数getMonthgetFullYeargetHoursgetMinutesgetSeconds Each of these function calls will independently use the Date object's UTC-based timestamp and the time zone configured on the computer where the code is being executed.这些函数调用中的每一个都将独立使用Date对象的基于 UTC 的时间戳和在执行代码的计算机上配置的时区。 If the code is executed in a web browser, then it's the user's computer's time zone that is applied.如果代码在 Web 浏览器中执行,则应用的是用户计算机的时区。 If the code is executed on the server, then it's the server's time zone that is applied.如果代码在服务器上执行,则应用服务器的时区。

Again, the important thing to remember is that Date object only track a single numeric value - the milliseconds since the Unix epoch (not accounting for leap seconds).同样,要记住的重要一点是Date对象只跟踪一个数值——自 Unix 纪元以来的毫秒数(不考虑闰秒)。 You can see this value directly by calling d.getTime() or d.valueOf() on the Date object (named d here), or by coercing the value to a Number as in +d .您可以通过在Date对象(此处命名为d d.valueOf()上调用d.getTime()d.valueOf()来直接查看此值,或者将值强制转换为数字,如+d The time zone is not part of the object's state, and it does not make network calls on its own.时区不是对象状态的一部分,它不会自行进行网络调用。

First of all its a very good question.首先,这是一个非常好的问题。 Lot of developers use date but never bother to go as to how it works exactly.许多开发人员使用 date 但从不费心去了解它是如何工作的。 Yes, it's machine based on which the JavaScript engine is running, so yes it depends upon timezone too.是的,它是基于运行 JavaScript 引擎的机器,所以是的,它也取决于时区。 See : https://javascript.info/date请参阅: https : //javascript.info/date

According to mdn, the exact date/hour is based on your computer.根据 mdn,确切的日期/小时取决于您的计算机。 More details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours更多详情: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours

Javascript runs on a web browser so it will take the time form the user's machine. Javascript 在 Web 浏览器上运行,因此它会占用用户机器的时间。 So if your users sit in different time zone then they will see different time.因此,如果您的用户坐在不同的时区,那么他们将看到不同的时间。

Javascript Date is executed on the client side ! Javascript 日期在客户端执行! So yes each user would see his machine time and his specific Timezone.所以是的,每个用户都会看到他的机器时间和他的特定时区。 Even when the user has a wrong time in his machine, he would see it wrong.即使用户在他的机器上有错误的时间,他也会看到它是错误的。

The exception returns Server time when you are using Node.js for example.例如,当您使用 Node.js 时,异常会返回服务器时间。 In that case, local time would be the server time.在这种情况下,本地时间将是服务器时间。 Again on servers, usually time and Timezones follow the OS time setting.再次在服务器上,通常时间和时区遵循操作系统时间设置。

If you expect them all to see the same time, Like displaying : "Time in London 10:13pm"如果您希望他们都看到同一时间,例如显示:“伦敦时间晚上 10:13”

You need to store your server time in a javascript variable using your server' programming language.您需要使用服务器的编程语言将服务器时间存储在 javascript 变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM