javascript - How to get the server time like new Date() gets the current time?
迷茫
迷茫 2017-06-28 09:27:55
0
1
536
let sd = 1498482000000;

let serverDate = () => new Date() > sd ? (
    new Date() - (new Date() - sd)
) : (
    new Date() + (sd - new Date())
)

console.log(serverDate()) // 1498482000000
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
巴扎黑
  1. Every time new Date() will get a different value, and the calculation will take time, it is recommended to use now cache: const now = new Date()

  2. now - (now - sd), the expansion is now - now + sd, that is, now + (sd - now), so the expressions of the two branches are equivalent, there is no need to write them at all Branch

  3. Finally it can be omitted into one sentence: new Date(sd) is the server time, why bother so much

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template