我制作了一个类组件来显示和呈现来自新的Date()函数的当前时间。我想要的是显示将现在的Date()减去将来的时间所剩下的时间量。假设现在是下午1点,将来的时间是下午3点,它将显示2:00:00。
这是我在当前时间的当前代码。
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h2>Waktu tersisa {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
任何帮助都将不胜感激。
发布于 2018-06-21 04:56:41
通过使用Moment.js
:
const currentDate = moment();
const future = moment('2019-03-02 10:03:02');
const timeLeft = moment(future.diff(currentDate)).format("HH:mm:ss");
有关详细说明,请参阅Moment.js。
https://stackoverflow.com/questions/50956516
复制相似问题