Moment.js set the base time from the server

前端 未结 3 750
难免孤独
难免孤独 2020-12-05 00:35

I have been reading the documentation, and cannot seem to find a method to set the date from the server as opposed to the client?

For example something like this

3条回答
  •  感动是毒
    2020-12-05 01:29

    Since Moment.js 2.10.7 it is possible to change the time source (see the PR that has introduced it).

    You can use it to synchronize the time Moment.js sees with your server's time.

    function setMomentOffset(serverTime) {
      var offset = new Date(serverTime).getTime() - Date.now();
      moment.now = function() {
        return offset + Date.now();
      }
    }
    

提交回复
热议问题