先安裝moment及moment-timezone
npm install moment --save npm install moment-timezone --save
const moment = require ('moment'); const tz = require('moment-timezone'); const timeString = "2019-04-07 02:00:00"; const timeString2 = "2019-04-08 02:15:00"; const timeString3 = "2019-04-07 02:00:03"; let timezone = "Asia/Tokyo"; let now = moment(timeString,'YYYY-MM-DD hh:mm:ss'); let now2 = moment(timeString2,'YYYY-MM-DD hh:mm:ss'); let now3 = moment(timeString3,'YYYY-MM-DD hh:mm:ss'); let jpTime = moment(timeString,'YYYY-MM-DDThh:mm:ss').tz(timezone); let jpTimeTz = moment.tz(timeString,'YYYY-MM-DDThh:mm:ss',timezone); console.log(now); console.log(now2); console.log(jpTime); console.log(jpTimeTz); console.log(now.diff(jpTime,'minutes')); console.log(now.diff(jpTimeTz,'hours')); console.log(now.diff(now2,'days')); console.log(now.diff(now3,'seconds'));
moment.diff支援誇時區計算