简体   繁体   中英

How can i get accurate date time according to the current time zone in angular

I want to get the exact date and time when user opens a page in his browser. I am trying to achieve this by using 'new Date()'.

 console.log(new Date());

Now, the issue is that it is picking user's machine time by default. For example, if I change my machine time manually, the output will be changed accordingly.

There are several ways to achieve this, 2 are listed below:

  1. Use a timeserver, for instance via ntp-time package

https://www.npmjs.com/package/ntp-time

  1. Use a server request (requires that you host your Angular SPA on a server):

Right way to get Web Server time and display it on Web Pages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM