moment(this.endEpoch).subtract(3, 'month').unix()
this.endEpoch
是今天的日期或发布时间:1484670569
这应该会产生一个仅仅3个月前的start_epoch日期,但它会创建一个负数:( -6467730
知道这里出了什么问题吗?
时刻()。减去('秒',1); //在2.8.0中弃用
时刻()。减去(1,'秒');
我尝试了两个版本,它们产生相同的负start_epoch编号。
答案 0 :(得分:3)
您传递的时间戳包含以秒为单位的值,但时刻也需要一毫秒,因此此代码应该正常工作:
moment(this.endEpoch * 1000).subtract(3, 'month').unix()
或使用unix()
方法:
moment.unix(this.endEpoch).subtract(3, 'month').unix()
答案 1 :(得分:1)
在以秒为单位使用unix时间戳时,您需要使用moment.unix(this.endEpoch)
。
答案 2 :(得分:1)
您应该使用moment.unix
代替moment(Number)
var endEpoch = 1484670569;
console.log(moment(endEpoch).format());
console.log(moment.unix(endEpoch).format());
var res = moment.unix(endEpoch).subtract(3, 'month').unix()
console.log(res);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
&#13;