Is there a time server that offers an API that i can grab the current eastern time from?

前端 未结 3 1766
说谎
说谎 2021-01-21 09:29

Ok we have a countdown on our site and I need to do an api call to get the current eastern time. I have this link which is the time I need. Is there any way or any webservice th

3条回答
  •  佛祖请我去吃肉
    2021-01-21 10:18

    The easiest answer is to just get the current time from your server, as this avoids cross-origin issues.

    • Every OS has a mechanism that syncs the local clock with a more reliable timekeeping source (such as NIST's atomic clock). If it's not already, configure your server to sync its clock with one of those sources.
    • It's relatively trivial to write a script in the language of your choice that returns the current time as a JSON object. Less trivial is ensuring that script returns the time in the proper time zone, accounting for DST (summer time) -- whether ET is UTC-5 or UTC-4. Many frameworks will take care of this for you, but some don't.

    Keep in mind that you still aren't (and can't) guaranteeing second-resolution accuracy. By requesting the time from your server, you're totally at the mercy of network conditions that you can't measure from JavaScript. One client might receive the AJAX time request in tens of milliseconds, while others with low-quality connections might even take tens of seconds.

    In fact, in many cases it might be more reliable to use the client's clock since it too will be synced to a reliable time source via NTP. I believe Windows has NTP sync enabled by default. Your best bet might be to make an AJAX request for the server's current time and date, and if the local machine's time is within 20 or so seconds of the server's time, just use local time.

提交回复
热议问题