Java/JavaScript dates: Is this true?

后端 未结 2 1545
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 11:33

So lets say a user is running my web app from his browser on a different time zone than the application server. I serialize a date on the client-side by using JavaScript\'s

2条回答
  •  无人及你
    2021-01-02 12:16

    There are some issues

    • The client is on different timezone. This is not really an issue if you use .getTime(), .valueOf() etc because they use ms since epoch according to UTC.
    • The real issue is that the client's clock can be skewed. If someone has set their computer back in time to avoid having to register some programs for example, it will cause the client to generate false timestamps
    • They can also have a small amount of skewness just because clocks are inaccurate like that

    You can counter this by sending a timestamp from your server and then comparing it to timestamp generated from the client, to get the skewness offset. You'd then apply this skewness offset to all new Date.getTime()s you generate on the client. Though this won't work if the client changes their system time as they are using your page.

提交回复
热议问题