JavaScript日期对象基于什么

huangapple go评论85阅读模式
英文:

Javascript date object based on what

问题

我正在使用下面的日期对象从用户那里获取当前时间:

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

我的问题是,currentdate 从哪里获取时间?它是从一个中央服务器获取时间吗?如果来自全球任何地方的用户同时执行上述代码,他们会获得相同的值吗?

还是说这是基于每个用户的,意思是如果来自不同时区的两个用户同时执行这些代码,它们会根据各自的时区获得时间,因此代码会返回不同的值?

英文:

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? 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?

答案1

得分: 3

在您提供的代码中,第一行是:

var currentdate = new Date();

Date 对象的构造函数在没有任何参数的情况下被调用,因此会使用与 Date.now() 相同值的时间戳进行初始化。该值是一个Unix时间戳,包含自1970年01月01日00:00:00.000 UTC以来的毫秒数(不考虑闰秒)。它严格基于协调世界时(UTC)。系统时区不参与构造,而 Date 对象不存储系统时区。

仅涉及系统的时钟。每台计算机系统都有一个内部时钟,可以返回当前的UTC时间。这个时钟通常通过NTP、PPTP或类似的机制与其他计算机的时钟同步。但它依赖于计算机。例如,当配置为“自动设置时间”时,Windows计算机将根据网络配置从Internet上的公共NTP服务器或本地网络上的域控制器获取时间。但是,未配置为自动设置时间的独立计算机可能具有非常不同的时钟值。

因此,如果两台给定的计算机调用new Date()(或Date.now()),如果它们的时钟正确同步,它们可能会检索到相似的值,但不保证。这里没有涉及网络调用,只是对本地系统时钟的调用。

在您的其余代码中,您调用了函数getMonthgetFullYeargetHoursgetMinutesgetSeconds。每个这些函数调用都会独立使用Date对象的基于UTC的时间戳以及代码执行所在计算机上配置的时区。如果代码在Web浏览器中执行,那么应用的是用户计算机的时区。如果代码在服务器上执行,那么应用的是服务器的时区。

再次强调的重要事情是,Date对象只跟踪一个单一的数值 - 自Unix纪元以来的毫秒数(不考虑闰秒)。您可以通过在Date对象(这里命名为d)上直接调用d.getTime()d.valueOf()来直接查看此值,或者通过将值强制转换为数字,如+d。时区不是对象的状态的一部分,它不会自行进行网络调用。

英文:

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(). That value is a Unix timestamp, containing milliseconds since 1970-01-01 00:00:00.000 UTC (not accounting for leap seconds). It is strictly UTC based. The sytem time zone is not involved in the construction, and the Date object does not store the system time zone.

Only the system clock is involved. Every computer system has an internal clock that can return the current UTC time. This clock is most often synchronized with clocks of other computers via NTP, PPTP, or similar mechanisms. 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. 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. 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. 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. If the code is executed in a web browser, then it's the user's computer's time zone that is applied. 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). 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. The time zone is not part of the object's state, and it does not make network calls on its own.

答案2

得分: 2

这基于Unix时间戳,并将基于用户计算机的时间(如果在客户端请求),或服务器时间戳(如果在服务器端请求)。

有关更多信息,请参阅:

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

英文:

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.

For further reading see:

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

答案3

得分: 0

首先,这是一个非常好的问题。许多开发者使用日期,但从未关心它的确切工作原理。
是的,它是基于 JavaScript 引擎运行的机器,因此它也依赖于时区。
参见:https://javascript.info/date

英文:

First of all its a very good question. Lot of developers use date but never bother to go as to how it works exactly.
Yes, it's machine based on which the JavaScript engine is running, so yes it depends upon timezone too.
See : https://javascript.info/date

答案4

得分: 0

根据 mdn,确切的日期/小时基于您的计算机。更多详细信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours

英文:

According to mdn, the exact date/hour is based on your computer. More details:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours

答案5

得分: 0

JavaScript 在 Web 浏览器上运行,因此它将使用用户机器上的时间。因此,如果您的用户位于不同的时区,他们将看到不同的时间。

英文:

Javascript runs on a web browser so it will take the time form the user's machine. So if your users sit in different time zone then they will see different time.

答案6

得分: 0

JavaScript Date在客户端执行!所以是的,每个用户都会看到他的机器时间和他特定的时区。
即使用户的机器时间错误,他也会看到错误的时间。

当您使用Node.js等服务器端技术时,异常会返回服务器时间。在这种情况下,本地时间将是服务器时间。
再次提醒,在服务器上,通常时间和时区遵循操作系统的时间设置。

如果您希望他们都看到相同的时间,比如显示:“伦敦时间晚上10:13”,
您需要使用您的服务器编程语言将服务器时间存储在JavaScript变量中。

英文:

Javascript Date is executed on the client side ! 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. 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"

You need to store your server time in a javascript variable using your server' programming language.

huangapple
  • 本文由 发表于 2020年1月7日 00:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定