如何根据旅行预订时间查找24小时差异

问题描述

从API中,我将Trip预订为时区(PST),日期时间(2020-07-10 16:00:00)。现在,我想检查与用户浏览器当前时间的24小时时差,并显示“取消”按钮,否则,我们需要隐藏该按钮。我在下面所做的是我正在通过API获取旅行预订时间-2020-08-10 16:58:00和预订时区-PST,然后找到currentDate并使用diff方法。但是我没有得到当前值。有帮助吗?

const departureDateWithTimeZone = moment.tz('2020-08-10 16:58:00','PST');
const currentDate = moment().format();
const diff = departureDateWithTimeZone.diff(currentDate,'h');

解决方法

Docs指出moment.tz中的最后一个参数应该是time zone identifier

使用America/Los_Angeles(或其他任何正确的tz标识符)代替PST

const departureDateWithTimeZone = moment.tz('2020-08-10 16:58:00','America/Los_Angeles'); 
const currentDate = moment(); 
const diff = departureDateWithTimeZone.diff(currentDate,'h');
  
console.log(`Current date: ${currentDate.format('YYYY-MM-DD HH:mm:ss Z')}`);
console.log(`Departure date: ${departureDateWithTimeZone.format('YYYY-MM-DD HH:mm:ss Z')}`);
console.log(`Diff in hours: ${diff}`); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js"></script>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...