我正在尝试获取下一个“6:00:00”的时间,我正在使用时刻:
let shiftEnd = moment("06:00:00", "HH:mm:ss")
console.log(shiftEnd.inspect())
它获取了前一个 6:00:00,即与 now()
同一天的时间。
moment("2017-07-31T06:00:00.000")
now()
是:
moment("2017-07-31T12:20:57.076")
获得尚未过去的下一个早上 6 点的最佳方式是什么?
最佳答案
您正在获取当天的时刻对象,因为时刻 Default部分说明:
You can create a moment object specifying only some of the units, and the rest will be defaulted to the current day, month or year, or 0 for hours, minutes, seconds and milliseconds.
您可以简单地测试您的时刻对象是否是过去的(使用 isBefore
)和 add
如果需要,1 天。
这是一个实例:
let shiftEnd = moment("06:00:00", "HH:mm:ss")
if( shiftEnd.isBefore(moment()) ){
shiftEnd.add(1, 'd')
}
console.log(shiftEnd.inspect())
console.log(shiftEnd.format())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
关于javascript - 获取尊重时间格式而不是上一个时间格式的下一个时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45414587/