为什么momentJS在这里给我一个负的start_epoch时间?

时间:2017-01-17 16:37:30

标签: javascript time momentjs

moment(this.endEpoch).subtract(3, 'month').unix()

this.endEpoch是今天的日期或发布时间:1484670569

这应该会产生一个仅仅3个月前的start_epoch日期,但它会创建一个负数:( -6467730

知道这里出了什么问题吗?

Moment Docs on substract

  

时刻()。减去('秒',1); //在2.8.0中弃用

     

时刻()。减去(1,'秒');

我尝试了两个版本,它们产生相同的负start_epoch编号。

3 个答案:

答案 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)

来源:https://momentjs.com/docs/#/parsing/unix-timestamp/

答案 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;
&#13;
&#13;