moment.jsを扱っていて、クライアントサイドアプリケーションでマシン依存しない時間を扱いたい場合の方法。
サーバからタイムスタンプを予め取得し、マシン時間との差分を保持しておく。 以降はその差分を計算することで正確な現在時刻を求める。
// timestampを返すAPI axios.get('/timestamp').then(timestamp => { serverTimeOffset = moment(timestamp, 'X').diff(moment(), 'seconds') })
let currentTime = moment() currentTime.add(serverTimeOffset, 'seconds')