Right now I'm getting a different number on my Desktop than I am on my laptop.

有帮助吗?

解决方案

getTime() uses the local time settings. The time settings can be changed in one of two places:

  1. Your OS (such as windows) may manage time; double click it on the taskbar
  2. Your OS relies on the system BIOS, it's one of the reasons that motherboards have a battery installed on the mobo (to keep time and settings in case of system failure), modifications to the time in your BIOS should be reflect in the OS, and consequently in JavaScript

An alternative is to make your own getTime function which pulls the time from one source, such as a server. If you want to minimize network calls, it might be worthwhile to pull this time once and at the same time make a call to getTime(). Then, at some other time, when it's needed, issue a getTime() again, subtract the difference and add it to the server time. Note: if time is important, I advise against this, since a user can easily alter their system clock

Otherwise, if your computers are on the same network, you can use a scheduler and a batch process to sync the times - it won't be perfect, but it'll be close enough

其他提示

Rather than use the javascript getTime() method, you should rethink your usecase.

If you're looking to timestamp a form submission (e.g. for a comment or form post) you should have the server generate the timestamp when handling the POST

If you want to have a consistent time client-side, consider making an ajax call to a simple web application that returns a timestamp. You could easily write one yourself, or you could use Yahoo's time service

Sync the clocks.

The OS should have an option to use a network time service. There could still be a slight difference, but it should not be more than a second or two, which is good enough for most purposes. How close do you need them to be?

getTime() gets the system time of the computer. However, if you really need to sync, it's best to sync with a web server that you have. Cheers!

You can't, at least not with JavaScript alone. JavaScript uses the system information on the client machine to determine what the time is. Now, if they're in different time zones, you can use the UTC() function to get them in UTC basis (seconds from midnight, Jan 1, 1970), but it's most likely that they just aren't synchronized.

That could be for a variety of reasons. The most likely causes:

  • The clocks might not be set identically. One (or both) could be wrong.
  • The clocks could be using different time zones.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top