根据最新时间计算过去的时分秒
constructor(props){ super(props); this.state = { date: Date.now()//获取当前时间戳 }; } componentDidMount() { this.timerID = setInterval( () =>this.tick(), 1000 );//设置定时器 } componentWillUnmount(){ clearInterval(this.timerID);//清除定时器 } tick(){ this.setState({ date: Date.now() }); } render() { const { card } = this.props; const { date } = this.state; const time = date - moment(card.startTime).valueOf();//计算时间差 let h = Math.floor(time / 1000 / 60 / 60 % 24);//获取时 let m = Math.floor(time / 1000 / 60 % 60);//获取分 let s = Math.floor(time / 1000 % 60);//获取秒 if(s < 10) { s = "0" + s; } if(m < 10) { m = "0" + m; } if(h < 10) { h = "0" + h; }//判断是否是个位数,自动补'0'