2017-11-25 189 views
4

我使用moment.js之間的差異不正確,我用下面的代碼做出反應器應用找到2 UNIX之間的區別時間戳Moment.js給予2個日期

import Moment from 'moment'; 

Moment.duration( 
    Moment(this.state.saleEndTime).diff(Moment(this.state.saleStartTime)) 
).humanize() 

其中

  • this.state.saleStartTime1511638810(週六,2017年11月25日十九時40分10秒GMT)
  • this.state.saleEndTime1516909110(星期四,2018年1月25日十九點38分30秒GMT)

但是它給輸出

an hour 

這顯然是不正確的,它應該是2個月。我做錯了什麼?

使用時刻v2.19.2與節點V7.9.0


編輯:需要將輸出humanize「版,並this.state.saleStartTimethis.state.saleEndTime之間的時間差可以從幾分鐘到幾個月...

+0

你不應該傳遞一個單元[DIFF](https://momentjs.com/docs /#/顯示/差值/)? –

+0

[獲得Moment Js中兩個日期之間的差異時間差異](https://stackoverflow.com/questions/25150570/get-hours-difference-between-two-dates-in-moment-js) – TheMintyMate

+0

@ Sag1v如果2個unix時間戳可以相距幾分鐘或幾個月,那麼我應該通過單元,並且輸出應該是「人性化」的? – Nyxynyx

回答

6

我沒有意識到這是一個unix郵票。
您應該使用unix方法,那麼:

moment.duration(
    moment.unix(1516909110).diff(moment.unix(1511638810)) 
).humanize(); 

運行段:

const result = moment.duration(
 
    moment.unix(1516909110).diff(moment.unix(1511638810)) 
 
).humanize(); 
 

 
const App =() => (
 
    <div > 
 
    <div>{result}</div> 
 
    </div> 
 
); 
 

 
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/moment.js"></script> 
 
<div id="root"></div>