Javascript 在React.Js组件中仅获取距离当前日期的秒数
嘿,我只想从当前日期中提取秒数,我有点被阻止了 这是我的组件,它只返回整个日期:Javascript 在React.Js组件中仅获取距离当前日期的秒数,javascript,reactjs,date,Javascript,Reactjs,Date,嘿,我只想从当前日期中提取秒数,我有点被阻止了 这是我的组件,它只返回整个日期: class Clock extends Component { constructor(props) { super(props); this.state = { time: new Date().toLocaleString() }; } componentDidMount() { this.i
class Clock extends Component {
constructor(props) {
super(props);
this.state = {
time: new Date().toLocaleString()
};
}
componentDidMount() {
this.intervalID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.intervalID);
}
tick() {
this.setState({
time: new Date().toLocaleString()
});
}
render() {
return (
<div>{this.state.time}.</div>
);
}
}
export default Clock;
类时钟扩展组件{
建造师(道具){
超级(道具);
此.state={
时间:新日期().toLocaleString()
};
}
componentDidMount(){
this.intervalID=setInterval(
()=>这个。勾选(),
1000
);
}
组件将卸载(){
clearInterval(此为有效期);
}
勾选(){
这是我的国家({
时间:新日期().toLocaleString()
});
}
render(){
返回(
{this.state.time}。
);
}
}
输出默认时钟;
您可以使用
getSeconds()日期对象上的代码>方法我不想转换为秒,我只想提取当前日期的当前秒数!您可以使用getSeconds();日期对象上的方法
constructor(props) {
super(props);
this.state = {
time: new Date().getSeconds()
};
}