尝试检查当前时间是否在开始时间和结束时间之间。
一切都在 UTC 中,有什么想法吗?
作品
shiftCurrentTime = moment.utc().format('hh:mm');
console.log(shiftCurrentTime);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
没用
shiftStartTime = moment.utc('20:00').format('hh:mm'),
shiftEndTime = moment.utc('05:00').format('hh:mm'),
shiftCurrentTime = moment.utc().format('hh:mm');
console.log(shiftCurrentTime.isBetween(shiftStartTime, shiftEndTime));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
最佳答案
你混淆了 moment 对象和它们的字符串表示(由 format()
给出)
你必须 parse您的字符串转换为指定格式的 moment 对象,因为 HH:mm
不是已知的 ISO 8601格式。
您可以调用isBetween
在 moment 对象上,在格式化字符串上调用它会给你错误。
这是一个完整的工作示例:
var shiftStartTime = moment.utc('20:00', 'HH:mm'),
shiftEndTime = moment.utc('05:00', 'HH:mm'),
shiftCurrentTime = moment.utc();
console.log(shiftCurrentTime.isBetween(shiftStartTime, shiftEndTime));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
关于javascript - 尝试使用 momentJS (UTC) 检查当前时间是否在开始/结束时间范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40343247/