好吧,我只是无法使用 moment.js 来增加日期。我在代码中得到一个 javascript 日期对象,将其包装到 moment
函数中,计算需要添加到初始日期的小时数,并在使用 .add
方法之后什么都没发生。尝试执行类似 currentTime.add(2, 'hours')
的操作,但效果不佳。我做错了什么?
const currentTime = moment(ioc.get<Main.IArchiveManager>("ArchiveManager").getCurrentDate());
const speed = this.getData().speed;
const distance = this.calcDistanceFromPrevPoint(initialPoint,prevPoint);
const timeToReachPoint = (distance / speed) * 60;
const estimatedTime = currentTime.add(timeToReachPoint, 'hours');
debugger;
return estimatedTime;
最佳答案
一切都按预期进行。您正在记录 currentTime
更改后的值。请记住,.add()
更改了对象的值,它不返回副本,而是返回对象本身(为了更好的链接)。请参阅我的示例,您会看到来的 console.log 被调用两次但在不同的时间显示您期望的值。
var time = moment(new Date());
console.log(time);
time.add(2,'h');
console.log(time)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
关于javascript - 无法使用 moment.js 增加日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920904/