DEV Community

What is the *legit* way to get the current date and time of system ?

Ankit Utekar on January 13, 2019

Yes, the title is a bit weird, but read on, I have an interesting story to tell. Recently at work I was tasked with implementing a timer. It wa...
Collapse
 
bitsrfr profile image
Joseph

Unfortunately, there's not really a better way to do it.

You can use a time API to get the global time. Essentially that pulls the time from a server that it synced with a global/atomic clock.

However, the time that returns may still have some delay depending on the network connection.

Collapse
 
bgadrian profile image
Adrian B.G.

One way would be to let the timer to be on server side and let it send you a push notification/trigger.

Other way would be to check the time on server and add the request time, from time to time.

When handling the time in a browser keep in mind

  • the interval can be negative, the user can change the hour, time minute and date during your timer
  • the browser can postpone/freeze your timers based on focus, CPU usage etc
Collapse
 
nektro profile image
Meghan (she/her)