0

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

Clinton Jooooones
  • 887
  • 3
  • 12
  • 19
  • 1
    Is the time different on your desktop and laptop? How much different are the values? Does it not correctly report the current time? ([NTP (Network Time Protocol)](http://en.wikipedia.org/wiki/Network_Time_Protocol) -- is often used for synchronizing time across machines/the internet; this has *nothing* to do with JavaScript.) –  Jun 17 '11 at 01:16

6 Answers6

2

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

ironchefpython
  • 3,409
  • 1
  • 19
  • 32
2

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

vol7ron
  • 40,809
  • 21
  • 119
  • 172
1

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?

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Within a second or two, but other people are going to be accessing the website and they need to be synced as well :/ – Clinton Jooooones Jun 17 '11 at 01:23
  • 2
    @user802519 Time to rethink the strategy. It is impossible to ensure that all people accessing the site will have their clocks within some small "acceptable" time delta. User time should be treated with caution as an external input -- the source is out of your control. –  Jun 17 '11 at 01:24
  • Is there a way to grab a time from an outside source that will be consistent for everyone? – Clinton Jooooones Jun 17 '11 at 01:28
  • 1
    @user802519: Your web server can use its own clock as "official". If someone posts a comment for example, the server needs to timestamp it (not the browser) before showing that data to other people. – Thilo Jun 17 '11 at 01:33
  • I need to display an dynamic countdown, can I grab the server time with javascript somehow? – Clinton Jooooones Jun 17 '11 at 01:39
0

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!

Vern
  • 2,393
  • 1
  • 15
  • 18
0

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.

brymck
  • 7,555
  • 28
  • 31
0

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.
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189